use*_*363 3 ruby-on-rails ruby-on-rails-3.1
我们的rails 3.1.0应用程序中有rfqs和引用控制器.Rfq有很多报价,引用belongs_to rfq.在routes.rb中,它是:
resources :rfq do
resources :quotes
end
Run Code Online (Sandbox Code Playgroud)
但是我们想列出索引中的所有引号.但预定义路由仅允许显示特定rfq的所有引号,如rake路由所示:
rfq_quotes GET /rfqs/:rfq_id/quotes(.:format) {:action=>"index", :controller=>"quotes"}
Run Code Online (Sandbox Code Playgroud)
添加路线的简单而干净的方法是什么,以便我们可以在索引中列出所有报价,并为列出的每个报价显示和编辑?非常感谢.
您必须将路线更改为:
resources :quotes, only: [:index]
resources :rfq do
resources :quotes
end
Run Code Online (Sandbox Code Playgroud)
并处理在这种情况下你不会有:rfq_id的事实.您可以使用之前的过滤器:
before_filter :load_rfq
def load_rfq
@rfq = Rfq.find(params[:rfq_id]) if params[:rfq_id].present?
end
Run Code Online (Sandbox Code Playgroud)
然后
def index
@quotes = @rfq.present? ? @rfq.quotes : Quote.all
end
Run Code Online (Sandbox Code Playgroud)