邮政表格上的自定义路线。 Rails 3.2.x

时间:2013-01-23 03:10:12

标签: ruby-on-rails routing routes

在浏览了一段时间之后,stackoverflow无法找到以下问题的解决方案:

我有一个脚手架模型fleets,我想将路线\fleets\postponed?id=31\与控制器中的特定操作匹配=>使用def postponed方法POST

虽然我试图遵循一些教程,但这里失败了:

Couldn't find Fleet with id=postpone

以下是我的路线:

  match 'fleets/postponed.id?:id', :controller => "fleets", :action => "postponed", :via => :post
  resources :fleets

以下是POST方法的表单:

 <%= form_for @fleet, :html => { :class => 'form-horizontal' }, :url => { :id => @fleet.id }  do |f| %>
   blah-blah-blah
 <%= f.submit 'Postpone' %>
 <% end %>

以下是行动:

def postponed
  @fleet = Fleet.find(params[:id])
  @fleet.update_attributes(params[:fleet])
end

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:2)

我认为你只需要在你的路线中

match 'fleets/postponed', :id => /d+/, :via => :post, :controller => 'fleets', :action => 'postponed'

答案 1 :(得分:1)

要实现此路线<host>/fleets/postponed?id=31,您应该拥有以下路线

match 'fleets/postponed/:id', :via => :post

请参阅The Query Strings

上的路由指南部分
match ':controller/:action/:id'

/fleets/postponed/1?user_id=2的传入路径将被分派到Fleets控制器的推迟动作。参数将是{ :controller => “fleets”, :action => “postponed”, :id => “1”, :user_id => “2” }