【问题标题】:How do I get my app to go to this custom view?如何让我的应用程序转到此自定义视图?
【发布时间】: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


【解决方案1】:

要查看哪些路由可在您的应用中使用,请在应用根目录的命令行中使用 rake routes。您应该会看到引用 upvoted_songs 的行。

然后像这样使用它:

<%= link_to "Upvoted Songs", upvoted_songs_video_path(video), :class => "upvoted_songs chosen_home_option" %>

因为你有一个 member 路由,所以 url 助手将获取一个视频对象(或 id)并生成一个看起来像这样的 url:/videos/7/upvoted_songs

但是,您的代码表明您可能正在做一些不依赖于单个视频对象的事情,并且 URL 中也不需要该对象。因此,您可能希望将该路由从 member 路由更改为 collection 路由。然后,该 URL 最终会看起来像 /videos/upvoted_songs,并且您不会向它传递视频对象。

希望这会有所帮助:)

第 2 部分

移除成员块:

resources :videos do
  resources :comments
  put 'topic_update', :on => :member
  get 'upvoted_songs', :on => :collection, :as => 'upvoted'
end

【讨论】:

  • 谢谢,如何将其更改为收集路线?这就是我想要的。
  • 查看下面由 coreyward 提供的答案,他在那里
  • @Justin 我为您发布了一个示例。当您忘记这些内容时,您也可以仔细检查路由指南。它相当全面。 guides.rubyonrails.org/routing.html
  • @ctcherry 知道 coreyward 代码中的这个错误是什么意思吗? ArgumentError can't use member outside resource(s) scope
  • 您输入的是member 还是:member?如果您可能错过了,它需要冒号。
【解决方案2】:

你没有身份证。做一个耙子路线 | grep upvoted 看看你的路线应该是什么样子。

可能类似于upvoted_songs_video_path(video)

【讨论】:

    【解决方案3】:

    您正在链接到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

    【讨论】:

    • 谢谢。你的代码给我抛出了这个错误:ArgumentError can't use member outside resource(s) scope 这是什么意思?
    • 确保您在传递给resources的块内调用了put 'topic_update', :on =&gt; :member。就像 ctcherry 在回应这个问题时提到的那样,确保你没有错过冒号。
    • 我更新了问题详细信息,提供了有关我的错误的更多详细信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    相关资源
    最近更新 更多