# Communication between Rails and Ajax

**URL:** <https://www.ruby-forum.com/t/communication-between-rails-and-ajax/195779>\
**Category:** Rails\
**Created:** [September 30, 2010, 12:56am UTC](https://www.ruby-forum.com/t/communication-between-rails-and-ajax/195779 "2010-09-30T00:56:19Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [September 30, 2010, 12:56am UTC](https://www.ruby-forum.com/t/communication-between-rails-and-ajax/195779/1 "2010-09-30T00:56:19Z")

</div>

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.
