Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RubyonRails从视图路由到控制器中的自定义方法_Ruby On Rails - Fatal编程技术网

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

Ruby on rails RubyonRails从视图路由到控制器中的自定义方法,ruby-on-rails,Ruby On Rails,我有一个控制器的名字。在我的/config/routes.rb中,我使用了这个- resources :posts /app/controllers/posts\u controller.rb: class PostsController < ApplicationController def new @post = Post.new end def show @post = Post.find(params[:i

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

resources :posts
/app/controllers/posts\u 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
/app/views/static\u pages/home.html.erb

我该怎么办?如果我使用“posts_path”,它将转到索引操作而不是categoryshow操作

# 我通读了链接,并从那里尝试了一些东西。以下是我面临的问题:

当我在config/routes.rb中尝试此操作时

resources :posts do

    collection do

          get 'categoryshow'

    end

end
这将生成“categoryshow_posts_path”

在我看来,我使用了以下方法:

<ul class="users">

     <%= Post::CATEGORIES.each do |category| %>

<li>

     <%= link_to category,categoryshow_posts_path %>

</li>

<% end %>

</ul>
结束

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

ActiveRecord::在PostsController类别Show中找不到RecordNot

        @post = Post.find(params[:category])
找不到没有ID的帖子


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

 match ':posts(/:categoryshow(/:category))'
在我看来,我使用的是:

类别

 <ul class="users">

 <%= Post::CATEGORIES.each do |category| %>

 <li>

     <%= link_to category,"posts/#{category}" %>

 </li> 

<% end %>

</ul>
结束

ActiveRecord::在PostsController#show中找不到RecordNot

找不到id=Politics的帖子

  • 我真的非常感谢这里的任何帮助

  • 谢谢(西德哈特)

请查看

或:

之后的一节将讨论在无法满足您的确切需求时添加任意路由的问题


请注意,routes文件是显式排序的:如果在其他内容捕获某个路由后尝试匹配该路由,则第一个路由将获胜。

请注意,如果该方法需要参数,则它是一个
集合
,否则,它是一个
成员
。第二种方法看起来更好,因为只需要添加一个新操作。当有2个或更多操作时,使用块。“不是吗?”克莱伯斯说。我对是否使用块感到矛盾;我发现第一个版本更容易推理,因为很明显它是什么类型的路线。有人帮忙吗??我添加了我尝试过的任何东西,但仍然无法解决问题。。
 <ul class="users">

 <%= Post::CATEGORIES.each do |category| %>

 <li>

     <%= link_to category,"posts/#{category}" %>

 </li> 

<% end %>

</ul>
        @post = Post.find(params[:id])
resources :posts do
  collection do
    get 'categoryshow'
  end
end
resources :posts do
  get 'categoryshow', :on => :collection
end