Render partial based on result changing
I'm trying to figure out how to accomplish a rendering over an existing rendered partial. Some background stuff first:
I have a table that is rendered based on data from DB. Outside of my rendered pa开发者_开发技巧rtial, I have various select tags which allow the data to update from on the specific select tag selected. I have tried to do the following, to no avail.
<%= select "sales", "short_desc", Sales.find(:all).collect{|x| [ x.short_desc]}, {:prompt => ''},
:onchange => remote_function(:url => {:controller => 'sales', :action => 'filterResults'},
:with => "'value=' + value") %>
so in the method filterResults, I have sql statement that returns a result, and I have tried to render the partial again.
render :partial => "tableReport", :locals => {:sales => @sales }
Any help would be most appreciated, thank you for taking the time to ready through my answer.
Regards~
You want to use AJAX I suppose? I would use RJS something like this:
In your controller:
def filterResults
@sales = ..
# logic
respond_to do |format|
format.html # renders the normal view if it's a normal request
format.js # renders filter_results.rjs if it's an ajax request
end
end
views/sales/filter_results.rjs
page.replace 'id_of_your_div', :partial => 'tableReport', :object => @sales
I haven't tested the code so it might need some changes. The idea is that you call filterResults with ajax and that it returns some javascript that replaces a div (or any other object with that #id) with the partial. You use view the Rails API to find more methods to manipulate the DOM through RJS.
精彩评论