3

I am using the jQuery UI slider to dynamically update items on a page. How can I let the controller receive the values from the slider? Is it possible to pass such information through as a local? If so, how would the local be handled by the partial

I am currently updating the whole page and using the where clause with the params.
@articles = Article.where(:year => params[start_year]...params[start_year])

I would like to just using jquery to empty this section and render a new collection of partials using the values set by the slider. Is there a best practice for this?

Oakland510
  • 1,073
  • 2
  • 12
  • 20

1 Answers1

0

I think the best option would be make an ajax call to a controller and pass in the paramaters through that.

Then, with the returned information, you should repopulate the parts of the page that need to be repopulated.

In your routes, you'll want something like this:

get "article/between_years", :controller=>"article", :action=>"between_years"

and in your controller, you'll have the function

def between_years
  @articles = Article.where(:year => params[start_year]...params[start_year])
  render :layout=>false ### this line makes it so the application.haml (or html.erb) is not rendered along with your code)
end

and in the directory app > views > articles, you'll have the haml, or erb file with the html you'll be wanting to add in.

then your jquery will look something like this:

$.get("/article/between_years",{start_year:1991,end_year:2011},function(data, status, xhr){  /// does ajax call to the article route we set up above /
  ..
  $("body").append(data);  ///appends the html data returned to the body
                           ///you will probably want to change this a bit
                           ///so every thing is added to the proper place
  ...
});

I wrote another answer a little while ago that outlined a simular work flow, you may want to take a look at that => Auto populate text_fields based on selected item from another collection_select in Rails 3

Community
  • 1
  • 1
BananaNeil
  • 10,322
  • 7
  • 46
  • 66
  • Hey @BananaNeil thanks for the quick response. I had to add a bit of jQuery in the between_years view to empty the body and append the articles. A separate call is made each time the slider, which was causing the article to append multiple times. For example, if you slid the end date from 2010 to 2005, it would post all articles from 2010-2011, 2009-2011, 2008-2011 and so on. Not sure my hack is still the best approach since multiple calls are still being made. I'm only preventing them in the view. – Oakland510 Dec 02 '11 at 07:52
  • I wouldn't consider it a hack at all. All depending on what you are wanting to replace exactly. You may even use $("body").html(data); which will completely replace the html of the body. If you can post more information, I would be happy to help you find a simple one call solution. – BananaNeil Dec 03 '11 at 00:35
  • I actually just realized that the UI slider offers both a "slide" AND a "stop" event. I switched from using the "slide" event to the "stop" event. This way, I'm only make one call once the user has stopped preforming the slide (VS making the call for each value the pass while using the slider). – Oakland510 Dec 03 '11 at 02:40