jeudi 9 juillet 2015

Can we fetch the data & assign it to a ruby variable through ajax?

I am using Rails 3.2 & Ruby 1.9.8

I have an ajax problem: On click of a radio button, I want to use ajax, get the result & pass the data to a query inside that same form.

Ex:

<%= radio_button(:type_id, type.id, :class => "radioButton", :remote => true) %> 

On click of this I will get an id of the radio button.

$(".radioButton").live("click",function(){
    var val_id = $(this).val();
    $.ajax({
        type: "GET",
        url: "/some_method",
        style : "inherit",
        dataType: 'script',
        data: {selected_id: val_id},
    }).done(function(data) {

    });
});

In controller:

def some_method
  @user = User.where(params[:selected_id])
  respond_to |format|
    format.json
  end
end

When done: The data will come back to the js file & will assign it to some div.

Then that div values I want to pass it to this partial:

<%= render 'some_partial', f: f, div_result: div_result %>

I am using semantic_nested_form_for before this partial. So I have to pass that builder also with the partial. & inside this partial there is a semantic_fields_for form.

Can we pass the div result to some ruby variable ? Is it possible?

I tried using js file from the controller & creating a js.erb file. But I could not access the bulder form data inside the partial.

Can anyone help? Please let me know if you need more info on this.

Aucun commentaire:

Enregistrer un commentaire