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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11178267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:35:30  来源:igfitidea点击:

Ruby on rails route to a custom method in the controller from the view

ruby-on-rails

提问by sidharth singh

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

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

resources :posts

/app/controllers/posts_controller.rb:

/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

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

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

What I want to do is to call -

我想做的是打电话 -

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

'categoryshow' method of posts controller from /app/views/static_pages/home.html.erb

帖子控制器的“categoryshow”方法来自 /app/views/static_pages/home.html.erb

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

我该如何管理?如果我使用“posts_path”,它将转到索引操作而不是 categoryshow 操作。

#

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

我通读了链接并从那里尝试了几件事。这是我面临的问题:

When I tried this in the config/routes.rb

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

resources :posts do

    collection do

          get 'categoryshow'

    end

end

This generates a 'categoryshow_posts_path'

这会生成一个“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:

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

def categoryshow

定义类别显示

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

end

结尾

In this case, I am getting the following error:

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

ActiveRecord::RecordNotFound in PostsController#categoryshow

ActiveRecord::RecordNotFound in PostsController#categoryshow

Couldn't find Post without an ID

找不到没有 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:

在视图中,我正在使用这个:

Categories

类别

 <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:

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

This is the show action:

这是表演动作:

def show

高清秀

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

end

结尾

ActiveRecord::RecordNotFound in PostsController#show

ActiveRecord::RecordNotFound in PostsController#show

Couldn't find Post with id=Politics

找不到 id=Politics 的帖子

  • I'd really appreciate any help here!!

  • Thanks (Sidharth)

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

  • 谢谢(悉达多)

回答by Dave Newton

Check out the "Adding More RESTful Actions" sections of the routing docs.

查看路由文档“添加更多 RESTful 操作”部分

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

Or:

或者:

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.

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