Communication between Rails and Ajax

Hey all,

I’m enduring an intense struggle. It involves when a user selects an
option from dropdown menu, it filters the fields. However, when I try
it, nothing happens. I would like to achieve this without any rails or
jquery plugins. The only plugin I would use is will_paginate and the
only javascript library I will use is jquery without any plugin.

When the user selects an option from dropdown, we call listLoader again
passing the data literal a value of data, which is the actual value of
the option element they selected (e.g. United States). This information
is therefore in the query string (or it should be) in the url. So we can
then use the params feature of Rails to capture it in controller.
listview.js
var listview = $(’#listview’);

$('#dashboard a').click(function(){listLoader(this);})


var listLoader = function(context, options){

    for(var i = 0; i < arguments.length; i++){
        var data = arguments[1]
    }

    var url = context.href.substring(context.href.lastIndexOf('#') +

1);

    $.ajax({
        type: 'GET',
        url: '/' + url,
        data: data, //this is converted to a query string
          dataType: 'html',
        error: function(){},
        beforeSend: function(){},
        complete: function() {},
          success: function(response) {
            listview.html(response);

            $("select").change(function(){
                var option = $(this).val();
                listLoader(context, option);
            })
            //end for change
        }
    })
}

//Here we check if there is a params, and if so, we capture the value of
the data parameter, call an instance method, which runs a sql statement
and then pass the value to @sites.
sites_controller.rb
def index
if params[:filter]
@sites = Site.filterize(params[:data])
else
@sites = Site.find(:all)
end

respond_to do |format|
  format.html {render :partial => "sites/index" }
end

end

//Below is the instance method
class Site < ActiveRecord::Base
belongs_to :country

def self.filterize(criteria)
Site.find_by_sql(“SELECT * FROM sites WHERE
country_id=#{criteria}.id”)
end
end

//Then we iterate through the instance method in our view:

Filter by: Default: <% Country.enabled_countries?("1").each do |c| %> <%= c.name %> <% end %>
<label>Search by:</label>
<input type="text" />

<input type="button" />
<% @sites.each do |i|%> "> <% end %>
Site
<%= i.site_num %>

Can anyone provide some insight as to what I’m missing here?

Thanks for any response.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs