Victor Coelho
Victor Coelho

Reputation: 15

Get params from controller using JQUERY RAILS

please help me i newbie on rails and jquery i try get all values from array #mychannels id element on html in my view.

JS AJAX

$(document).ready(function() {
  $('#mychannels').change(function () {
    $.ajax({
      type: "GET",
      contentType: "dados/json",
      url: "/suporte/chamados?empresa_id=91194",
      success: function(data){
        alert(data);
      }
    });
  });
});

Controller

  def get_data
    @company = Company.find(params[:company_id])
    @servers = Server.find(:all, :conditions => ["company_id = ? AND action != 3", @company.id])
    @channels_list = Channels.where("channel = 't' and company_id = 91194")

My View

<%= select_tag "mychannels",options_for_select(@channels_list.map { |e|
                                 [e.name+" - "+e.server.name, e.id]})  %>

I am trying to read the data that comes from the controller throw to an array and display it in the select_tag of the view.

could you help me with the code

Upvotes: 0

Views: 212

Answers (1)

Adri&#224; Carro
Adri&#224; Carro

Reputation: 717

You need to update your controller action adding the render method and adding the values you want to get in your jQuery success callback.

Controller

def get_data
  @company = Company.find(params[:company_id])
  @servers = Server.find(:all, :conditions => ["company_id = ? AND action != 3", @company.id])
  @channels_list = Channels.where("channel = 't' and company_id = 91194")
  render json: { company: @company.to_json, servers: @servers.to_json, channels_list: @channels_list.to_json }
end

Upvotes: 3

Related Questions