如何让我的应用转到此自定义视图?

我在我的视频控制器中有这个自定义操作:
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" %>
    
已邀请:
要查看应用程序中可用的路径,请在应用程序根目录的命令行中使用
rake routes
。你应该看到一条引用
upvoted_songs
的线。 然后像这样使用它:
<%= link_to "Upvoted Songs", upvoted_songs_video_path(video), :class => "upvoted_songs chosen_home_option" %>
由于你有一个成员路由,url helper将获取一个视频对象(或id)并生成一个类似于以下内容的URL:
/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
    
您正在链接到
videos_path
,这是“videos#index”的帮助者。 正如ctcherry所解释的那样,您当前的路线是使用成员路线而不是集合。以下是您正在寻找的更多内容:
resources :videos do
  put 'topic_update', :on => :member
  get 'upvoted_songs', :on => :collection, :as => 'upvoted'
end
然后你可以使用
upvoted_videos_path
代替
videos_path
。     
你没有身份证。做耙路线| grep upvoted,看看你的路线应该是什么样子。 它可能像
upvoted_songs_video_path(video)
    

要回复问题请先登录注册