model - Rails routing when nesting resources -
model - Rails routing when nesting resources -
i'm trying figure out how specify route i'm trying utilize maintain getting routing errors. can please point out i'm going wrong , maybe explain improve way might be?
i'm working these (nested) resources:
resources :users resources :playlists end models:
class user < activerecord::base has_many :playlists end class playlist < activerecord::base belongs_to :user validates :user_id, presence: true end now i'm trying link user's playlists within user/show.html.erb file:
<p> <%= link_to "playlists", user_playlists_path(@playlist)%></p> </p> which brings me playlists page (/users/1/playlists) when seek adding new playlist user, next error:
showing /app/views/playlists/_form.html.erb line #1 raised: undefined method `playlists_path' #<#<class:0x0000000335c688>:0x00000003d0b238> this line #1:
<%= form_for(@playlist) |f| %> this rake routes if helps @ all:
prefix verb uri pattern controller#action user_playlists /users/:user_id/playlists(.:format) playlists#index post /users/:user_id/playlists(.:format) playlists#create new_user_playlist /users/:user_id/playlists/new(.:format) playlists#new edit_user_playlist /users/:user_id/playlists/:id/edit(.:format) playlists#edit user_playlist /users/:user_id/playlists/:id(.:format) playlists#show patch /users/:user_id/playlists/:id(.:format) playlists#update set /users/:user_id/playlists/:id(.:format) playlists#update delete /users/:user_id/playlists/:id(.:format) playlists#destroy users /users(.:format) users#index post /users(.:format) users#create new_user /users/new(.:format) users#new edit_user /users/:id/edit(.:format) users#edit user /users/:id(.:format) users#show patch /users/:id(.:format) users#update set /users/:id(.:format) users#update delete /users/:id(.:format) users#destroy root / default_pages#home signup /signup(.:format) users#new signin /signin(.:format) users#signin
the errors:
undefined method `playlists_path' #<#<class:0x0000000335c688> and
user_playlist /users/:user_id/playlists/:id(.:format)
gives clear reason of error have.
<%= form_for(@playlist) do|f| %> should
<%= form_for([@user, @playlist]) do|f| %> or current_user, of import need pass user object.
explanation :
if have noticed in update action (taking redirect show page after update) instead of redirect_to user_path(@user) can redirect_to @user, rails infers redirecting show path of user.
it similar situation here, if having form_for playlist , pass @playlist instead of [@user, @playlist] woud seek find new_playlist_path, isn't in route , show error.
this short gist on how might it.
ruby-on-rails model rails-routing activeresource
Comments
Post a Comment