Ruby 2.0.0, Rails 4.0.3
I have a _new partial. But, I render it with an instance that actually exists. This is necessary so that I can pass the instance ID around through JavaScript between the View and the Controller, since I cannot actually pass the instance itself. I simply call Class.first in the new method so that I have an existing instance to use through the process.
My problem is that the _new partial submit button recognizes that the instance already exists. This causes it to update instead of create. The button literally says update. When pressed, it routes to the update method. That is not what I want. I want the create method, where I'll create a new instance populated with the gathered parameters.
What to do? Am I wrong in just carrying a dummy instance to start the process? If so, what is the correct solution? If this is otherwise acceptable, how do I force the button to create instead of update?
All assistance and comments appreciated.
EDIT: I've tried variations on the button to try to force it to trigger to the new method. It continues to fire to update. My last failed effort is:
<button type="submit" formaction="new_admin_car_path" class="btn btn-default btn btn-primary">Create Car</button>
...End Edit...
The form is:
<div class="span8">
<% car_id = @car.id %>
<%= simple_form_for [:admin, @car],
defaults: {label: false},
html: {id: 'new_admin_car', class: 'form-vertical', method: post},
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_admin_car', car: @car, value: nil}, autocomplete: :off, placeholder: 'Stock number?'}) %>
<%= f.input(:ymm_year_id, {input_html: {form: 'new_admin_car', car_id: car_id, value: nil}, collection: YmmYear.all.order("year desc").collect{|c| [c.year, c.id]}, prompt: "Year?"}) %>
<%= render partial: "makes", locals: {form: 'new_admin_car', car_id: car_id} %>
<%= render partial: "models", locals: {form: 'new_admin_car', car_id: car_id} %>
<%= f.association(:color, {input_html: {form: 'new_admin_car', value: nil}, autocomplete: :off, prompt: 'Color?'}) %>
<div class="col-xs-6 col-sm-3">
<br/>
<input type="submit" form="new_admin_car" value="Create Car" class="btn btn-default btn btn-primary">
<% end %>
</div>
</div>
Makes partial:
<% unless car_id.blank? %>
<% car = Car.find(car_id) %>
<%# car.ymm_make_id = nil %>
<%= simple_form_for [:admin, car],
defaults: {label: false},
remote: true do |f| %>
<% makes ||= "" %>
<% make = "" %>
<% make = car.make_id if car.class == Car and Car.exists?(car.id) %>
<% if !makes.blank? %>
<%= f.input :ymm_make_id, {input_html: {form: form, car: car, car_id: car.id, value: make}, collection: makes.collect { |s| [s.make, s.id] }, prompt: "Make?"} %>
<% else %>
<%= f.input :ymm_make_id, {input_html: {form: form, car: car, car_id: car.id, value: make}, collection: [], prompt: "Make?"} %>
<% end %>
<% end %>
<% end %>
Controller Car Method New where clear just nils all fields:
def new
@car = Car.first
@car.clear
end
Rendered form:
JavaScript for the form is:
// when the #year field changes
$("#car_ymm_year_id").change(function () {
// make a GET call and replace the content
// First select identifies what has been selected, or fired
var year = $('select#car_ymm_year_id :selected').val();
// Pull the variables from the input_html tag
var form = $('select#car_ymm_year_id').attr("form");
var car_id = $('select#car_ymm_year_id').attr("car_id");
// Routes to the controller action
$.post('/admin/cars/make_list/',
{
form: form,
year: year,
car_id: car_id
},
function (data) {
$("#car_ymm_make_id").html(data);
});
return false;
});
Controller method:
def make_list
makes = YmmMake.makes(params[:year])
#@car = Car.find(params[:car_id])
render partial: "makes", locals: {car_id: params[:car_id], form: params[:form], makes: makes}
end