Ruby on rails 从视图路由到控制器中的自定义方法

编程入门 行业动态 更新时间:2024-10-28 00:22:02
本文介绍了Ruby on rails 从视图路由到控制器中的自定义方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个控制器名称帖子.在我的 /config/routes.rb 中,我使用了这个 -

I have a controller name posts. In my /config/routes.rb, I have used this -

resources :posts

/app/controllers/posts_controller.rb:

class PostsController < ApplicationController def new @post = Post.new end def show @post = Post.find(params[:id]) end def categoryshow @post = Post.find(params[:category]) end def index @posts = Post.all end def create @post = Post.new(params[:post]) if @post.save flash.now[:success] = "Your Post Successful" redirect_to @post else render 'new' end end end

我是 Rails 的新手,我经常对路线感到困惑.我有另一个 static_pages 控制器.其中有一个 home.html.erb 文件.

I am new to rails and I often get confused with routes. I have another static_pages controller. In there is a home.html.erb file.

我想做的是打电话-

def categoryshow @post = Post.find(params[:category]) end

来自/app/views/static_pages/home.html.erb

我该如何处理?如果我使用posts_path",它会转到索引操作而不是 categoryshow 操作.

How do I manage that? If I use 'posts_path', it goes to index action instead of categoryshow action.

我通读了链接并从那里尝试了一些东西.这是我面临的问题:

I read through the link and tried a couple of things from there. Here is the problem that I am facing:

当我在 config/routes.rb 中尝试这个时

When I tried this in the config/routes.rb

resources :posts do collection do get 'categoryshow' end end

这会生成一个categoryshow_posts_path"

This generates a 'categoryshow_posts_path'

在我看来,我使用了这个:

In my view, I used this:

<ul class="users"> <%= Post::CATEGORIES.each do |category| %> <li> <%= link_to category,categoryshow_posts_path %> </li> <% end %> </ul>

我的帖子控制器有以下方法:

My posts controller has the following method:

定义类别显示

@post = Post.find(params[:category])

结束

在这种情况下,我收到以下错误:

In this case, I am getting the following error:

ActiveRecord::RecordNotFound in PostsController#categoryshow

ActiveRecord::RecordNotFound in PostsController#categoryshow

无法找到没有 ID 的帖子

Couldn't find Post without an ID

其次,我尝试使用您提供的链接中提到的非资源丰富的路由:

Secondly, I tried out using non resourceful routes as mentioned in that link you provided:

match ':posts(/:categoryshow(/:category))'

在视图中,我使用的是这个:

In the view, I am using this:

类别

<ul class="users"> <%= Post::CATEGORIES.each do |category| %> <li> <%= link_to category,"posts/#{category}" %> </li> <% end %> </ul>

在这种情况下,我们的非资源路由只有在没有其他现有资源路由匹配时才会匹配.但是,我看到显示操作已匹配,并且收到此错误消息:

In this case, our non resourceful route will match only if no other existing resourceful route matches. However, i am seeing that show action is matched and I get this error message:

这是表演动作:

定义显示

@post = Post.find(params[:id])

结束

ActiveRecord::RecordNotFound in PostsController#show

ActiveRecord::RecordNotFound in PostsController#show

找不到 id=Politics 的帖子

Couldn't find Post with id=Politics

  • 我真的很感谢这里的任何帮助!!

  • I'd really appreciate any help here!!

谢谢(Sidharth)

Thanks (Sidharth)

推荐答案

查看 添加更多 RESTful 操作"路由文档的部分.

resources :posts do   collection do     get 'categoryshow'   end end

或者:

resources :posts do get 'categoryshow', :on => :collection end

之后的部分讨论了在不满足您的确切需求的情况下添加任意路由.

The section after that discusses adding arbitrary routes if that doesn't meet your exact needs.

请注意,路由文件是明确排序的:如果您在其他东西捕获它之后尝试匹配它,则第一个路由获胜.

Note that the routes file is explicitly ordered: if you try to match a route after something else would have captured it, the first route wins.

更多推荐

Ruby on rails 从视图路由到控制器中的自定义方法

本文发布于:2023-10-26 10:52:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529923.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   视图   路由   器中   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!