传递参数Rails 3时没有路由匹配

时间:2011-04-26 08:14:36

标签: routes ruby-on-rails-3

尝试获取路由,以便传递一个字符串,我可以在控制器中使用该字符串来查询数据库。

 controller:
 def sales
@sales= Micropost.where(:sale_name => params[:sale])
@sales = Sale.sales
 end

View:
<%= link_to('See all', :controller =>"microposts", :action => "sales", :sale => sales_item.sale_name) %>

Routes.rb
match '/sales/:sale', :to => 'microposts#sales'

1 个答案:

答案 0 :(得分:0)

将路线更改为match 'microposts/sales' => 'microposts#sales',您可以将参数传递给该操作。

You can also make it a named route,看起来像match 'sales' => 'microposts#sales', :as => :sales。有了这个,您将获得sales_urlsales_path。您可以将其用作例如sales_path(:sale => sales_item.sale_name)

希望这有帮助。