我在视频控制器中有这个自定义操作:
def upvoted_songs
@votes = current_user.videos_votes.where("value = 1")
@videos = @votes.videos.page(params[:page]).per(15)
end
这是我的路线:
resources :videos do
member do
put 'topic_update'
get 'upvoted_songs'
end
end
我的视频索引视图中的此链接:
<%= link_to "Upvoted Songs", videos_path, :action => "upvoted_songs", :class => "upvoted_songs chosen_home_option" %>
和一个名为videos / upvoted_songs.html.erb。
的视图文件为什么链接不是指向upvoted_songs.html.erb视图,而是保留在视频索引视图中?
更新:
这是我的routes.rb:
root :to => "videos#index"
resources :video_votes
resources :videos do
resources :comments
member do
put 'topic_update', :on => :member
get 'upvoted_songs', :on => :collection, :as => 'upvoted'
end
end
resources :comment_titles
resources :users
resources :profiles
resources :genres
resources :topics
resources :topicables
resource :session
我最初收到此错误:
ArgumentError
can't use member outside resource(s) scope
然后刷新页面后,我收到此错误:
ActionController::RoutingError in Videos#index
Showing /rubyprograms/dreamstill/app/views/videos/_video.html.erb where line #22 raised:
No route matches {:action=>"show", :id=>#<Video id: 485, title: "I'm Ready For You", description: nil, embed_code: nil, thumbnail_url: nil, released: nil, user_id: 57, created_at: "2011-04-02 08:47:36", updated_at: "2011-04-09 22:42:48", video_url: "http://www.youtube.com/watch?v=wy86KNtOjVg", video_votes_count: 0, vote_sum: 3, rank_sum: 28927.724512>, :controller=>"videos"}
22: <%= link_to video.title, video_path(video), :class => "normal" %>
答案 0 :(得分:3)
要查看可在您的应用中使用的路由,请在应用根目录的命令行中使用rake routes
。您应该看到引用upvoted_songs
的行。
然后像这样使用它:
<%= link_to "Upvoted Songs", upvoted_songs_video_path(video), :class => "upvoted_songs chosen_home_option" %>
由于您拥有成员路由,因此网址助手将获取视频对象(或ID)并生成类似于以下内容的网址:/videos/7/upvoted_songs
但是,您的代码表明您可能正在做一些不依赖于单个视频对象的事情,并且也不需要在URL中使用它。因此,您希望将该路由从成员路由更改为集合路由。然后,URL最终会看起来像/videos/upvoted_songs
,并且您不会将其传递给视频对象。
希望这会有所帮助:)
第2部分
删除成员块:
resources :videos do
resources :comments
put 'topic_update', :on => :member
get 'upvoted_songs', :on => :collection, :as => 'upvoted'
end
答案 1 :(得分:2)
您正在关联videos_path
,这是“视频#index”的帮手。
正如ctcherry所解释的那样,您当前的路线是使用成员路线而不是集合。以下是您正在寻找的内容:
resources :videos do
put 'topic_update', :on => :member
get 'upvoted_songs', :on => :collection, :as => 'upvoted'
end
然后,您可以使用upvoted_videos_path
代替videos_path
。
答案 2 :(得分:1)
你没有身份证。做耙路线| grep upvoted,看看你的路线应该是什么样的。
它可能类似于upvoted_songs_video_path(video)