The idea is: when I select a "product" I want to show the price of
that product automatically in the "price" text-field. I have a table
where I store the price of each product.
Why would you think prototype code would work in a jquery app? What
version of rails are you using? What have you done to setup your app
for jquery? Why did you think that information was unnecessary? Aren't
you aware that rails changes every week?
$("#sale_product_id").change(function() {
var product_id = $(this).val();
//this is equal to the html element
//$(this) turns the html element into a jquery object
//jquery objects have a val() method, which sets or
//gets the value attribute of an html element
var query_string = "product_id=" + product_id;
$.get('get_price/', //action name
query_string, //e.g "product_id=3"
function(data){ $("#sale_price").val(data)
//data equals what the get_price action returns
});
});
});
</script>
..
...
app/controllers/sales_controller.rb
class SalesController < ApplicationController
def do_stuff
@sale = Sale.new
@products = Product.all
end
def get_price
@price = Product.find(params[:product_id]).price
render :text => @price
end