我想弄清楚如何指定我嘗試使用的路由,但我不斷收到路由錯誤。有人可以指出我哪裏出錯了,也許解釋什麼是更好的方法來做到這一點?嵌套資源時的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