AhmedShawky
AhmedShawky

Reputation: 910

form_for issue GET request instead of patch

I have edit form for company as bellow in edit.html.erb

  <%= form_for([:dashboard , @company]  ) do |f| %>
      <%= f.text_field :name , :class => "form-control"   %>
      <%= f.submit "Save" , :class => "btn btn-primary"%>
  <% end %>

and my companies_controller.rb

    def edit
     @company = Company.find(params[:id])
    end

    def update
      # Update code
    end

and my routes.rb

   namespace :dashboard do
       resources :companies , only: [ :edit , :update ]
   end

The problem is when submit the form get the bellow error

   No route matches [GET] "/dashboard/companies/3"

Upvotes: 0

Views: 391

Answers (1)

Pavan
Pavan

Reputation: 33542

form_for accepts a post method by default.Here the edit action is get method,so your form_for should look like this

<%= form_for([:dashboard , @company],:html => {:method => :get }) do |f| %>
      <%= f.text_field :name , :class => "form-control"   %>
      <%= f.submit "Save" , :class => "btn btn-primary"%>
  <% end %>

OR

You can do like this too

<%= form_for([:dashboard , @company] :url =>edit_dashboard_company_path(@company)) do |f| %>
      <%= f.text_field :name , :class => "form-control"   %>
      <%= f.submit "Save" , :class => "btn btn-primary"%>
  <% end %>

Upvotes: 1

Related Questions