无法找到id = edit的歌曲

时间:2013-07-23 05:55:41

标签: ruby-on-rails ruby-on-rails-4

尝试编辑歌曲时,我收到以下错误:

ActiveRecord::RecordNotFound in SongsController#show
Couldn't find Song with id=edit
Extracted source (around line #69):


def set_song
@song = Song.find(params[:id])
end
def song_params

要访问代码库/简易故障排除,请访问:www.github.com/apane/leap

为了我刚刚安装并让CanCan工作但不幸的是,我随后遇到了上述错误。

歌曲(资源)路线:

 songs GET    /songs(.:format)               songs#index
                         POST   /songs(.:format)               songs#create
                new_song GET    /songs/new(.:format)           songs#new
               edit_song GET    /songs/:id/edit(.:format)      songs#edit
                    song GET    /songs/:id(.:format)           songs#show
                         PATCH  /songs/:id(.:format)           songs#update
                         PUT    /songs/:id(.:format)           songs#update
                         DELETE /songs/:id(.:format)           songs#destroy
             songs_index GET    /songs/index(.:format)         songs#index
              songs_show GET    /songs/show(.:format)          songs#show
               songs_new GET    /songs/new(.:format)           songs#new
              songs_edit GET    /songs/edit(.:format)          songs#edit
                 contact GET    /contact(.:format)             songs#contact
                     faq GET    /faq(.:format)                 songs#faq
                    root GET    /                              songs#index

2 个答案:

答案 0 :(得分:1)

您遇到路由冲突:

resources :songs

get "songs/index"
get "songs/show"
get "songs/new"
get "songs/edit"  

我猜您在尝试按照songs/edit路径时收到了错误消息。好吧,它上面的resources :songs声明会覆盖它,调用标识为show的{​​{1}},与错误消息匹配。

删除所有"path"声明,因为它们是不必要的; get用RESTful路由覆盖它们。

答案 1 :(得分:1)

songs / index.html.erb 视图中您有:

<% @songs.each do |song| %>
  <%= link_to('Edit', edit_song_path(@song), class: "button small secondary") if can? :update, @song %>
<% end %>

链接应该是:

<%= link_to('Edit', edit_song_path(song), class: "button small secondary") if can? :update, song %>

song不是@song

和routes.rb

如果你有:

resources :songs

你不需要:

  get "songs/index"
  get "songs/show"
  get "songs/new"
  get "songs/edit"