ashwinm ashwinm - 4 months ago 12
Ruby Question

Undefined method with “_path” while using simple_form_for

I am having problem with routing when using simple_form_for. I understand it is something to do with pluralizing but the same form is working perfectly for edit/update method.

undefined method `companies_path'
Did you mean? companys_path
company_path


companys_controller

def new
@company = Company.new
end

def create
@company = Company.new(company_params)

if @company.save
redirect_to @company
else
render 'new'
end

end


_form.html.haml

= simple_form_for @company do |f|
= f.input :name
= f.input :description
= f.input :website

= f.button :submit


rake routes

companys GET /companys(.:format) companys#index
POST /companys(.:format) companys#create
new_company GET /companys/new(.:format) companys#new
edit_company GET /companys/:id/edit(.:format) companys#edit
company GET /companys/:id(.:format) companys#show
PATCH /companys/:id(.:format) companys#update
PUT /companys/:id(.:format) companys#update
DELETE /companys/:id(.:format) companys#destroy


I guess the problem is, Edit/update is going for 'companys_path' and new/create is going for 'companies_path'. Any help is appreciated.

Answer

Controller name should be plural so in your case it should be companies_controller

Follow these steps to change it

  • Rename controller to companies_controller.rb
  • Change controller class name to

    class CompaniesController < ActionController::Base
      # controller code
    end
    
  • Change in routes.rb

    resources :companies