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

发布于 2024-10-31 16:47:57 字数 1812 浏览 2 评论 0原文

我的视频控制器中有这个自定义操作:

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" %>

I have this custom action in my videos controller:

def upvoted_songs
  @votes = current_user.videos_votes.where("value = 1")
  @videos = @votes.videos.page(params[:page]).per(15)
end

this is my routes:

resources :videos do
    member do
      put 'topic_update'
      get 'upvoted_songs'
    end
end

And this link in my videos index view:

<%= link_to "Upvoted Songs", videos_path, :action => "upvoted_songs", :class => "upvoted_songs chosen_home_option" %>

and a view file called videos/upvoted_songs.html.erb.

Why doesn't the link direct to the upvoted_songs.html.erb view but rather stay on the video index view?

UPDATE:

These is my 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

I initially get this error:

ArgumentError

can't use member outside resource(s) scope

Then after refreshing the page, I get this error:

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" %>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三生一梦 2024-11-07 16:47:57

要查看哪些路由可在您的应用中使用,请在应用根目录的命令行上使用rake paths。您应该会看到一行引用 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 中使用该对象。因此,您希望将该路由从成员路由更改为集合路由。该 URL 最终会看起来像 /videos/upvoted_songs ,并且您不会向其传递视频对象。

希望这会有所帮助:)

第 2 部分

删除会员块:

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

To see what routes are available for use in your app use rake routes on the command line in the root directory of the app. You should see a line that refers to upvoted_songs.

Then use it like so:

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

Since you have it has a member route the url helper will take a video object (or id) and generate a url that looks something like: /videos/7/upvoted_songs

However, your code suggests that you might be doing something that does not rely on a single video object, and wouldn't need that in the URL either. So you would want to change that route from a member route to a collection route. The URL would then end up looking something like /videos/upvoted_songs and you wouldn't be passing it a video object.

Hope this helps :)

PART 2

Remove the member block:

resources :videos do
  resources :comments
  put 'topic_update', :on => :member
  get 'upvoted_songs', :on => :collection, :as => 'upvoted'
end
安静被遗忘 2024-11-07 16:47:57

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

You are linking to videos_path, which is a helper for "videos#index".

As ctcherry explained, your current route is using a member route and not a collection. The following is more what you're looking for:

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

Then you can use upvoted_videos_path in place of just videos_path.

苏别ゝ 2024-11-07 16:47:57

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

它可能类似于 upvoted_songs_video_path(video)

You don't have an id. Do a rake routes | grep upvoted and see what your route should look like.

it's probably something like upvoted_songs_video_path(video)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文