【问题标题】:Rails UrlGeneration Error on form_for after upgrade to 4.2升级到 4.2 后 form_for 上的 Rails UrlGeneration 错误
【发布时间】:2015-03-01 00:16:12
【问题描述】:

我刚刚将 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 时,错误就开始了。

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

编辑:

这也是我的 rake 路线的相关部分:

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

【问题讨论】:

  • 我遇到了类似的问题。我正在为form_for 使用自定义 ActiveModel 表单对象,并且由于从 4.1 升级到 4.2 Rails 生成路由的方式不同——它的 HTTP 方法正确,但端点错误——它正在向 @ 提交 PATCH 987654328@ 什么时候应该转到/users/:id,视情况而定。通过将表单对象上的#to_model 委托给我正在包装的 AR 模型,这在 4.1 中运行良好,但现在我遇到了路由错误。你有没有发现这个问题?
  • 啊,刚刚浏览了源代码,发现了一个解决我问题的错误。 This commit 修复了它,但它是在 4.2.0 版本之后出现的,他们还没有发布下一个补丁版本。 IDK 是否可以解决您的问题,但可能值得在 rails master 分支上运行以查看是否可以解决问题。

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


【解决方案1】:

我也有类似的问题。

我稍微修改了 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-06
    • 2015-02-21
    • 2015-03-14
    • 2014-05-22
    • 1970-01-01
    • 1970-01-01
    • 2015-03-17
    相关资源
    最近更新 更多