【发布时间】:2011-08-03 13:22:09
【问题描述】:
我的视频控制器中有这个自定义操作:
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 视图,而是停留在视频索引视图上?
更新:
这是我的路线.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" %>
【问题讨论】:
-
只是好奇:@votes = current_user.videos_votes.where("value = 1") 不应该 > 1 吗? :P
-
否,因为我想检索用户投票的所有视频,并且在我的应用程序中投票值为 1 :)
-
我明白了,我以为你想获得所有赞成票。但是布尔值不是比 1 更好吗?只是再问一次:)
-
hmm 我不确定这将如何工作,我有 1 的原因是,如果它是一个赞成票,价值 = 1,如果它是一个反对票,价值是 -1,如果用户决定删除他的投票,值为0。然后总结总投票值很容易。
-
哦,太好了,如果我不得不做那个功能,一定要记住这一点
标签: ruby-on-rails ruby ruby-on-rails-3 redirect hyperlink