我想弄清楚如何指定我尝试使用的路由,但我不断收到路由错误。有人可以指出我哪里出错了,也许解释什么是更好的方法来做到这一点?嵌套资源时的Rails路由
我与这些(嵌套)资源:
resources :users do
resources :playlists
end
型号:
class User < ActiveRecord::Base
has_many :playlists
end
class Playlist < ActiveRecord::Base
belongs_to :user
validates :user_id, presence: true
end
现在我想链接到用户的播放列表的用户/ show.html内。 ERB文件:
<p>
<%= link_to "Playlists", user_playlists_path(@playlist)%></p>
</p>
这让我想起一个播放列表页面(/用户/ 1 /播放列表)成功,但是当我尝试添加NE W¯¯播放该用户,我得到以下错误:
Showing /app/views/playlists/_form.html.erb where line #1 raised:
undefined method `playlists_path' for #<#<Class:0x0000000335c688>:0x00000003d0b238>
这是线#1:
<%= form_for(@playlist) do |f| %>
这是我得到rake routes
有没有什么帮助所有:
Prefix Verb URI Pattern Controller#Action
user_playlists GET /users/:user_id/playlists(.:format) playlists#index
POST /users/:user_id/playlists(.:format) playlists#create
new_user_playlist GET /users/:user_id/playlists/new(.:format) playlists#new
edit_user_playlist GET /users/:user_id/playlists/:id/edit(.:format) playlists#edit
user_playlist GET /users/:user_id/playlists/:id(.:format) playlists#show
PATCH /users/:user_id/playlists/:id(.:format) playlists#update
PUT /users/:user_id/playlists/:id(.:format) playlists#update
DELETE /users/:user_id/playlists/:id(.:format) playlists#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
root GET / default_pages#home
signup GET /signup(.:format) users#new
signin GET /signin(.:format) users#signin
有道理,谢谢。我试图用[@user,@playlist]替换@playlist,并得到相同的错误。它还能是什么? – user2804881 2014-11-04 05:11:27
将它从@playlist更改为“[:user,@playlist]”工作。但现在当我提交新的播放列表时,它会抱怨更多的播放列表路由。有很多“@playlist”的地方......是不是有一些方法可以生成正确的路径,这样我就不必去修复所有这些错误的路径了? – user2804881 2014-11-04 05:22:08
你的playlist_controller.rb有新的用户吗?更新代码或在gist.github.com上搜索代码,向我们展示的越少,我们可以帮助的东西越少:) – argentum47 2014-11-04 05:23:40