I have such jquery code:
jQuery(function($) {
$(document).ready(function () {
$('.lol').each(function(index) {
console.log("#"+$(this).attr("id"));
$.ajax({
url: "/articles/get_prices/nr="+$(this).attr("nr")+"&br="+$(this).attr("br")+"&type="+$(this).attr("type"),
type: "GET",
data: {},
success: function(text)
{
$("#"+$(this).attr("id")).html(text);
},
error: function(){
alert('Ошибка javascript');
},
dataType : "html"
});
});
});
});
and such haml code
.box{:id => art.ART_ARTICLE_NR.gsub(/[^0-9A-Za-z]/, '')}
method
def get_prices()
nr = params[:nr]
br = params[:br]
type = params[:type]
@pr = find_price(nr, br, type)
respond_to do |format|
format.html { render :partial=>"search_trees/price" }
end
end
i need to set this div jquery value, but it is all dynamic, so after page loads, it go to db, and via id (which is another for each box) set it a value.
try this, your js file:
(you do an ajax request with script datatype, so the response gets executed and you don’t need to handle that by yourself)
your controller:
(add the instance variable @id and render a js view since the ajax request is ‘script’ type)
your view (search_trees/price.js.erb, this is important that you submit a js file)
(render the html view and set the html of the div)
you might want to add some checks to do the alert if something goes wrong (like… you can’t fine the @pr object), so you cover both cases:
in your view: