升级到4.2后,在form_for上出现Rails UrlGeneration错误

时间:2015-01-02 17:23:10

标签: ruby-on-rails ruby-on-rails-4 routes form-for

我刚刚将rails从4.0升级到4.2,这似乎导致我的一个form_fors出错。现在,当我访问新视图时,我得到:

错误:

ActionController::UrlGenerationError at /billing/providers/new
No route matches {:action=>"show", :controller=>"billing/provider_agencies", :id=>nil} missing required keys: [:id]

查看(此行的错误):

= form_for @provider_agency, url: billing_provider_agency_path(@provider_agency) do |f|
  ...

路线:

namespace :billing do
  resources :provider_agencies, path: "providers" do
    resources :invoices
  end
end

控制器:

class Billing::ProviderAgenciesController < BillingController
  before_action :set_provider_agency, only: [:show, :edit, :update, :destroy]

  def index
    ...
  end

  def show
    @users = @provider_agency.users
  end

  def destroy
    ...
  end

  def new
    @provider_agency = Agency::Provider.new
  end

  def create
    ...
  end

  def edit
    ...
  end

  def update
    ...
  end

  protected
  def provider_agency_params
    params.require(:agency_provider).permit(:id, :name,...
    ...
    ])
  end

  def set_provider_agency  
    @provider_agency = @agency.agencies.find(params[:id])
  end
end

我必须在form_for中定义url,因为我在路由中命名资源的方式。定义form_for中的url用于在包括new的任何视图中工作。现在,新视图似乎在调用show动作,并查找尚未创建/保存的@provider_agency。要清楚,这打破了新观点,但所有其他观点仍然有效。

我回去检查提交,但这些文件都没有改变,当我升级到Rail 4.2时,错误就开始了。

非常感谢任何想法,谢谢!

编辑:

以下是我的佣金路线的相关部分:

billing_provider_agencies     GET    /billing/providers(.:format)           billing/provider_agencies#index
                              POST   /billing/providers(.:format)           billing/provider_agencies#create
new_billing_provider_agency   GET    /billing/providers/new(.:format)       billing/provider_agencies#new
edit_billing_provider_agency  GET    /billing/providers/:id/edit(.:format)  billing/provider_agencies#edit
billing_provider_agency       GET    /billing/providers/:id(.:format)       billing/provider_agencies#show
                              PATCH  /billing/providers/:id(.:format)       billing/provider_agencies#update
                              PUT    /billing/providers/:id(.:format)       billing/provider_agencies#update
                              DELETE /billing/providers/:id(.:format)       billing/provider_agencies#destroy

1 个答案:

答案 0 :(得分:0)

我有类似的问题。

我修改了form_for语法。

对于嵌套模型,旧语法:

form_for(@child, url: parent_child_path(@parent,@child)) 

更新语法

form_for([@parent,@child]) 

对于独立模型

form_for(@model, url: model_path(@model)) 

form_for(@model) 

参考here