jeudi 27 août 2015

form_for won't pass id Rails 3.2

I have the following strange situation:

I call my edit action through a link_to,

<%= link_to 'Edit', companies_edit_path(:id => company.id)%>

And everything is fine. I mean, i reach the edit page with the correct dates loaded. This is my edit page:

<%= form_for @company ,:url => {:action => :update, :controller => :companies, :method => :put} do |f| %>
  <% if @company.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@company.errors.count, "error") %> prohibited this order from being saved:</h2>
      <ul>
      <% @company.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>


<form class="form-horizontal" role="form">
<div class="form-group">

                        <label class="control-label col-sm-2" for="name">name:</label>
                        <div class="col-sm-10">
                            <%= f.text_field :name, size: 40 %>
                        </div>
                    </div>
...
...
</form>
<% end %>

i took off all the things into the form_for, I don't think they are important. Now , when i click on submit i should go in my update action (as you can see in the form_for declaration). But it said to me that isn't able to find the company cause it doesn't have id passed. this is my controller:

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


def update
        @company = Company.find(params[:id])
    respond_to do |format|
      if @company.update_attributes(params[:company])
        format.html { redirect_to companies_index_path, :notice => 'Aggiornato.' }
        format.json { head :no_content }
      else
        format.html { render :action => "edit" }
        format.json { render :json => @company.errors, :status => :unprocessable_entity }
      end
    end
  end

these are my routes:

  get  "companies/index"
  get  "companies/new"
  get  "companies/search"
  get  "companies/edit"
  post "companies/create"
  put  "companies/update"
  devise_for :users
  resource :calendar, :only => [:show]
  resources :events
  root :to => 'home#index'

Do you see anything strange?

Aucun commentaire:

Enregistrer un commentaire