如何将自定义参数添加到路由中的默认操作

时间:2011-09-23 16:29:38

标签: ruby-on-rails ruby-on-rails-3.1 rails-routing

你能帮帮我吗?我卡住了!

我为模型“Customer”实现了单个表继承。 “人”和“公司”是“客户”。所以我添加了两条新路由来将所有请求转发到CustomersController

resources :customers                             # added by generator
resources :people,    :controller => 'customers' <== NEW
resources :companies, :controller => 'customers' <== NEW

我想做的是

  1. 将参数“type”添加到资源的操作“new”:customers
  2. 将默认值“Person”和“Company”添加到其他资源中的“type”参数中(如果参数继承!?)
  3. 我的目标是能够致电

    new_customer_path(:type => 'Person')
    

    new_person_path
    

    之前我尝试过以下操作,但它停止了其他操作(如show)

    resources :people, :controller => 'customers' do
      get 'new', :on => :member, :type => 'Person'
    end
    

    那里的任何人都可以告诉我我的错误吗?

2 个答案:

答案 0 :(得分:2)

尝试将参数添加到资源参数

resources :people,    :controller => 'customers', :type => "Person"
resources :companies, :controller => 'customers', :type => "Company"

答案 1 :(得分:0)

至于new_person_path,你可以这样做:

map.new_person "new_person", :controller => "customers", :action => "new", :type => "person"

请记住:

new_customer_path(:type => "person")

会在网址中传递“?type = person”,以便访问者能够更改它。