-1

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;
});
Richard_G
  • 4,700
  • 3
  • 42
  • 78
  • Do you mean passes the var from controller to view? Or is it where you make the GET call in the JS? – Aryess Aug 28 '14 at 09:41
  • @car comes to the view from the controller via normal methods. When year changes, JS fires and passes the selected year back to the controller so that it can build a list of makes for that year. I need the car instance passed back from the view to the controller through JavaScript when that fires. – Richard_G Aug 28 '14 at 09:48
  • @Aryess I think my comment was misdirected. See above. – Richard_G Aug 28 '14 at 10:04

2 Answers2

0

One way or another, all JS can send/receive is strings. You need to encode your @car object in JSON when answering to the POST call, and parse it in your callback function.

In the controller:

@car = {} # do whatever you have to do to find the corresponding car
respond_to do |format|
  format.html
  format.json { render :json => @car.to_json }
end

In your view, don't forget to add a .json at the end of the path you are calling in your POST request. In that case I believe it would be /admin/cars/make_list.json, but you may have to adapt.

Then in your JS output your data var in the console to check if it's a raw string or if it's an object. I don't remember if jQuery automatically parse it or not. If it's a raw string, pass it to JSON.parse(your_var) to have a valid object (as seen here)

Community
  • 1
  • 1
Aryess
  • 530
  • 2
  • 12
0

In the end, the entire concept is a bad idea. I asked another question that provided a good solution.

Creating an instance with dependent selects

Community
  • 1
  • 1
Richard_G
  • 4,700
  • 3
  • 42
  • 78