Ruby 2.0.0, Rails 4.0.3
When the year select is triggered, JavaScript currently passes the name of the instance variable (@car) from the view to the controller as a string. I need to pass a reference to the instance variable instead of just the string name. Is this possible? I'd appreciate the help.
EDIT: I've tried replacing the reference to car as follows, but I receive a syntax error.
var car = $("#<%= @car %>");
Error: Syntax error, unrecognized expression: #<%= @car %>
The view is:
<div class="span8">
<%= simple_form_for [:admin, @car],
defaults: {label: false},
html: {id: 'new_admin_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| %>
<%= f.input(:stock_number, {input_html: {form: 'new_admin_car', car: @car}, autocomplete: :off, placeholder: 'Stock number?'}) %>
<%= f.input(:ymm_year_id, {input_html: {form: 'new_admin_car', car: @car}, collection: YmmYear.all.order("year desc").collect{|c| [c.year, c.id]}, prompt: "Year?"}) %>
<%= render partial: "makes", locals: {form: 'new_admin_car', car: @car} %>
<%= render partial: "models", locals: {form: 'new_admin_car', car: @car} %>
<input type="submit" form="new_admin_car" value="Create Car" class="btn btn-default btn btn-primary">
<% end %>
</div>
The current script 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 = $('select#car_ymm_year_id').attr("car");
// Routes to the controller action
$.post('/admin/cars/make_list/',
{
form: form,
year: year,
car: car
},
function (data) {
$("#car_ymm_make_id").html(data);
});
return false;
});