kevinweaver
kevinweaver

Reputation: 523

Rails UrlGeneration Error on form_for after upgrade to 4.2

I just upgraded rails from 4.0 to 4.2 and it seems to have caused an error in one of my form_fors. Now when I visit the new view, I get:

Error:

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

View (error from this line):

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

Routes:

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

Controller:

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

I had to define the url in the form_for because of the way I namespace the resources in my routes. Defining that url in the form_for used to work in any view including new. Now the new view seems to be calling the show action, and looking for the @provider_agency that hasn't been created/saved yet. To be clear, this breaks the new view but all other view's still work.

I went back to check the commits and none of these files have changed, the error simply begins when I upgrade to Rail 4.2.

Any ideas are greatly appreciated, thanks!

EDIT:

Here is the relevant portion of my rake routes too:

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

Upvotes: 2

Views: 715

Answers (1)

Prasanna
Prasanna

Reputation: 11544

I had a similar issue.

I modified the form_for syntax little bit.

For nested model,old syntax:

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

to newer syntax

form_for([@parent,@child]) 

For independent model

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

to

form_for(@model) 

Refer here

Upvotes: 0

Related Questions