Question

Environment is Ruby 2.0.0, Rails 4.0.3, Windows 8.1, Simple Form, IE or Firefox.

I am having trouble getting HTML input type=submit to fire in the edit action. The new action is very nearly the same and it works consistently. In this case, I have eliminated almost all inputs and submenus on the form to simplify this particular issue.

When I click on the submit button in the new action, it fires the controller create action. When I click on the submit button in the edit action, literally nothing happens. No trace information occurs in the log or in the client console of any action whatsoever. The controller update action is not fired. In both cases, breakpoints show that @car is referencing the appropriate car object.

I am using standard HTML for the submit because the form button never works with the complex menus I am building. I use the form attribute to associate the button with the form and have triple-checked to ensure that the form specified in the HTML input tag matches that in the form, either new_car or edit_car.

The controller actions are:

  def edit
    @car = Car.find(params[:id])
  end
  def new
    @car = Car.new
  end

Routing is defined using:

  resources :cars

new.html.erb:

<div class="row">
  <%= render partial: 'new', layout: 'layouts/sf_label', locals: { title: 'New Car' } %>
</div>

edit.html.erb:

<div class="row">
  <%= render partial: 'edit', layout: 'layouts/sf_label', locals: { title: 'Edit Car' } %>
</div>

The working "new" partial form is:

<div class="span8">
  <%= simple_form_for @car,
                      defaults: {label: false},
                      html: {class: 'form-vertical'},
                      wrapper: :vertical_form,
                      wrapper_mappings: {
                              check_boxes: :vertical_radio_and_checkboxes,
                              radio_buttons: :vertical_radio_and_checkboxes,
                              file: :vertical_file_input,
                              boolean: :vertical_boolean
                      } do |f| %>
      <%= f.input(:stock_number, {input_html: {form: 'new_car', car: @car},  autocomplete: :off, placeholder: 'Stock number?'}) %>
      <input type="submit" form="new_car" value="Create Car" class="btn btn-default btn btn-primary">
  <% end %>
</div>

The failing "edit" partial is:

<div class="span8">
  <%= simple_form_for @car,
                      defaults: {label: false},
                      html: {class: 'form-vertical'},
                      wrapper: :vertical_form,
                      wrapper_mappings: {
                              check_boxes: :vertical_radio_and_checkboxes,
                              radio_buttons: :vertical_radio_and_checkboxes,
                              file: :vertical_file_input,
                              boolean: :vertical_boolean
                      } do |f| %>
      <%= f.input(:stock_number, {input_html: {form: 'edit_car', car: @car}, autocomplete: :off, placeholder: 'Stock number?'}) %>
      <input type="submit" form="edit_car" value="Update Car" class="btn btn-default btn btn-primary">
  <% end %>
</div>

The HTML form and button for new_car is:

<input type="text" value="" placeholder="Stock number?" name="car[stock_number]" id="car_stock_number" form="new_car" class="string required" car="#&lt;Car:0x9830ef8&gt;">

<input type="submit" class="btn btn-default btn btn-primary" value="Create Car" form="new_car">

The HTML form and button for edit_car is:

<input type="text" value="AStock" placeholder="Stock number?" name="car[stock_number]" id="car_stock_number" form="edit_car" class="string required" car="#&lt;Car:0x62ebd08&gt;">

<input type="submit" class="btn btn-default btn btn-primary" value="Update Car" form="edit_car">

Gemfile is:

source 'https://rubygems.org'
ruby '2.0.0'
gem 'rails', '4.0.3'
gem 'sass-rails', '~> 4.0.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jquery-turbolinks'
gem 'jbuilder', '~> 1.2'
gem 'bootstrap-sass'
gem 'figaro'
gem 'pg'
gem 'simple_form', '3.0.1'
gem 'thin'
gem 'devise'
gem 'cancan'
gem 'rolify'
gem 'acts_as_tenant'
gem 'jquery-datatables-rails', github: 'rweng/jquery-datatables-rails'
gem 'jquery-ui-rails'
group :production do
  gem 'rails_12factor'  # For Heroku
  end
group :development do
  gem 'better_errors'
  gem 'binding_of_caller'
  gem 'guard-bundler'
  gem 'guard-rails'
  gem 'guard-rspec'
  gem 'hub', :require=>nil
  gem 'quiet_assets'
  gem 'rails_layout'
  gem 'rb-fchange', :require=>false
  gem 'rb-fsevent', :require=>false
  gem 'rb-inotify', :require=>false
end
group :development, :test do
  gem 'factory_girl_rails'
  gem 'rspec-rails'
end
group :test do
  gem 'database_cleaner', '1.0.1'
  gem 'email_spec'
end

Generated form tag for new_car:

<form novalidate="novalidate" method="post" id="new_car" class="simple_form form-vertical" action="/cars" accept-charset="UTF-8">

Generated form tag for edit_car:

<form id="edit_car_53" class="simple_form form-vertical" novalidate="novalidate" method="post" action="/cars/53" accept-charset="UTF-8">
Était-ce utile?

La solution

As requested by Graeme McLean, I entered the HTML form tags into the question. There, I noted that the form attribute was not edit_car, as I expected but instead was edit_car_[:id], for example edit_car_53. As a result, the submit button was disconnected from the form and would not fire. To resolve this problem, I updated the form to just use edit_car, as follows:

  <%= simple_form_for @car,
                      defaults: {label: false},
                      html: {id: 'edit_car', class: 'form-vertical'},
                      wrapper: :vertical_form,
                      wrapper_mappings: {
                              check_boxes: :vertical_radio_and_checkboxes,
                              radio_buttons: :vertical_radio_and_checkboxes,
                              file: :vertical_file_input,
                              boolean: :vertical_boolean
                      } do |f| %>

This resolved that particular problem, thanks to the many that responded and especially Graeme.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top