0

I want send selected drop down menu value to controller by ajax

panel_controller.rb

class PanelController < ApplicationController

      def insert
         @city_ids = params[:city]
      end
end

panel.js.erb

$(document).ready(function() {
    $('#f_city_id').change(function() {
        var city_js_id = this.value
        $.ajax({
            url: '/panel/insert',
            type: 'GET',
            data: {"city": city_js_id},
            success: function (data,status)
            {
                alert(this.url);
            }
        });
        return false;
    });
});

routes.rb

get '/panel/insert' => 'panel#insert'

views/panel/insert.html.erb

<%= @city_ids %>

but @city_ids dont respond value after chenge drop down menu

2
  • data: {"city": city_js_id} should be data: {city: city_js_id} Commented Jun 20, 2016 at 11:11
  • @Emu I change to {city: city_js_id} but dont work Commented Jun 20, 2016 at 11:14

1 Answer 1

1

You need to respond back from your insert method.

Try doing this

class PanelController < ApplicationController
  def insert
    @city_ids = params[:city]
    respond_to do |format|
      format.html { render partial: 'insert.html.erb' }
    end
  end
end

Create a partial file with the new content _insert.html.erb

<%= @city_ids %>

In you panel.js.erb try catching the response and append it in your DOM wherever necessary. Your updated value will be on the page.

$(document).ready(function() {
  $('#f_city_id').change(function() {
    var city_js_id = this.value
    $.ajax({
        url: '/panel/insert',
        type: 'GET',
        data: {"city": city_js_id},
        success: function (res){
            $("#somediv").html(res);
            //You will get the partial's content with the new data and you'll only need to append it to your page.
        }
    });
    return false;
  });
});
Sign up to request clarification or add additional context in comments.

5 Comments

I try this But dont work ==> Error {Template is missing}
You can give the full path. Lets say your partial resides inside views/panel/_inser.html.erb. Do render partial: 'panel/insert.html.erb'
Look at the error, it must be giving you the path where it didn't find. You'll be able to fix it. :)
i want show in my view not in partial and i fix path but There are error
Yes. The partial will be rendered with the updated information from the server so new information will show on your view. What error do you get?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.