0

尝试编辑歌曲时出现以下错误:

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
4

2 回答 2

1

您有路由冲突:

resources :songs

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

我猜您在尝试遵循songs/edit路径时收到了错误。好吧,resources :songs它上面的声明覆盖它,show使用 id调用"path",匹配错误消息。

删除所有这些get声明,因为它们是不必要的; resources :songs用 RESTful 路由覆盖它们。

于 2013-07-23T06:02:02.680 回答
1

song/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"
于 2013-07-23T06:05:49.300 回答