Rails:自定义操作的 model_url

发布于 2024-09-07 08:00:49 字数 1038 浏览 3 评论 0原文

假设我有简单的 Rails 博客应用程序。

我有一个自定义操作,例如 page_views,它显示帖子的浏览次数。

class PostsController < ApplicationController

  def page_views
     #show some page views
  end

end

app/views/Posts 文件夹中还有一个关联的视图。

现在,在 routes.rb 中,我有:

map.resources :posts

map.resources :posts, :collection => {
                                      :page_views=> :get
                                      }

指向 page_views 视图的链接:

link_to("View Page Views",page_views_posts_path + "/" + post.id.to_s)

在我的帖子 show.html.erb 文件中,我有一个 paths

page_views_posts_path(post)
page_views_path(post)
page_views_posts(post)

要么导致找不到方法,要么导致网址不正确,例如:

http://localhost:3000/posts/page_views.#<posts:0xabcdef00>

我假设网址应该是:

http://localhost:3000/posts/page_views/1

那么,我在这里缺少什么?

Let's say I have the simple rails blog app.

And I have a custom action, like page_views which shows the number of views of the post.

class PostsController < ApplicationController

  def page_views
     #show some page views
  end

end

And there is also an associated view in the app/views/Posts folder.

Now, in the routes.rb I have:

map.resources :posts

map.resources :posts, :collection => {
                                      :page_views=> :get
                                      }

in my posts show.html.erb file I have a link to the page_views view:

link_to("View Page Views",page_views_posts_path + "/" + post.id.to_s)

Another paths:

page_views_posts_path(post)
page_views_path(post)
page_views_posts(post)

Have either resulted in method not found or an incorrect url, like:

http://localhost:3000/posts/page_views.#<posts:0xabcdef00>

I would assume the url should be:

http://localhost:3000/posts/page_views/1

So, what I am missing here?

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

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

发布评论

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

评论(2

℉絮湮 2024-09-14 08:00:49

如果您想为每个视图提供 page_views 页面,您应该声明额外的操作,而不是作为集合方法,而是作为成员方法:

map.resources :posts, :member => { :page_views=> :get }

此外,如果您也希望所有帖子都这样做(显示某种排序表) )添加与收集操作相同的参数:

map.resources :posts, :member => { :page_views=> :get }, :collection => { :page_views => :get }

这样您将生成以下路由:

page_views_post_path(post) # for single post
page_views_posts_path      # for all posts

您可以通过运行以下命令来检查新路由:

$ rake routes | grep page_views

考虑到您尚未为其他控制器声明它们,您将仅获得那些关联的视图。

If you want to provide page_views page for each view you should declare extra action not as collection method but as a member method:

map.resources :posts, :member => { :page_views=> :get }

Also if you want this for all posts as well (show the ranking table of some sorts) add the same parameter as collection action:

map.resources :posts, :member => { :page_views=> :get }, :collection => { :page_views => :get }

This way you'll have following routes generated:

page_views_post_path(post) # for single post
page_views_posts_path      # for all posts

You can check new routes by running following command:

$ rake routes | grep page_views

You will get only those associated views considering the fact that you haven't declared them for other controllers.

心在旅行 2024-09-14 08:00:49

您还可以向 routes.rb 中的资源添加自定义方法,如下所示:

resources :posts do 
  collection do
    get :page_views
  end
end

并使用 page_views_posts_path 访问自定义方法。

You can also add custom methods to resources in routes.rb like this:

resources :posts do 
  collection do
    get :page_views
  end
end

And use page_views_posts_path to access to the custom method.

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