嵌套路由后路径被搞砸了

时间:2012-12-21 16:32:31

标签: ruby-on-rails

我真的不知道为什么会这样。

这是我的路线

resources :users do
    resources :bookmarks
end

控制器

# bookmarks_controller
def edit 
    # this returns perfectly data in the edit view
    @bookmark = current_user.bookmarks.find(params[:id])
end

这是视图

<%= form_for @bookmark do |b| %>
<% end %>

由于当我嵌套用户内的书签资源时一切都搞砸了,导致错误

未定义的方法`bookmark_path'

然后我将表单更改为

<%= form_for user_bookmark_path(current_user, @bookmark) do |b| %>

错误已消失,但文字字段表单中没有此类数据,表单操作为 / users / [user_id] / bookmarks / [bookmark_id] / edit

耙路线信息

user_bookmarks     GET    /users/:user_id/bookmarks(.:format)          bookmarks#index
                   POST   /users/:user_id/bookmarks(.:format)          bookmarks#create
new_user_bookmark  GET    /users/:user_id/bookmarks/new(.:format)      bookmarks#new
edit_user_bookmark GET    /users/:user_id/bookmarks/:id/edit(.:format) bookmarks#edit
user_bookmark      GET    /users/:user_id/bookmarks/:id(.:format)      bookmarks#show
                   PUT    /users/:user_id/bookmarks/:id(.:format)      bookmarks#update
                   DELETE /users/:user_id/bookmarks/:id(.:format)      bookmarks#destroy

知道怎么解决吗?感谢

1 个答案:

答案 0 :(得分:4)

尝试类似:

<%= form_for [current_user, @bookmark] do |b| %>
<% end %>