Hoan Dang
Hoan Dang

Reputation: 2280

The path was messed up after nested routing

I really have no clue why it happen.

this is my routes

resources :users do
    resources :bookmarks
end

Controller

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

This is the view

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

Since everything messed up when I nested the bookmarks resources inside users it causes the error

undefined method `bookmark_path'

Then I change the form to

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

The error is gone but there is no such data in the text field form, and the form action is /users/[user_id]/bookmarks/[bookmark_id]/edit

rake routes info

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

Any idea how to fix it ? Thanks

Upvotes: 0

Views: 75

Answers (1)

Jiř&#237; Posp&#237;šil
Jiř&#237; Posp&#237;šil

Reputation: 14402

Try something like:

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

Upvotes: 4

Related Questions