jQuery:
No gem needed. Copy the rails.js file here:
https://github.com/rails/jquery-ujs/blob/master/src/rails.js
and put it the public/javascripts directory. Name the file
jquery.rails.js. Note how the javascript_include_tag below links to
that file:
app/views/layouts/application.htm.erb:
<!DOCTYPE html>
<html>
<head>
<title><%= @title %></title>
<%= csrf_meta_tag %>
<% javascript_include_tag(
"http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js",
"jquery.rails.js"
)
%>
<script type="text/javascript">
$(document).ready(function () {
$("#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
end
app/models/sale.rb:
# == Schema Information
#
# Table name: sales
#
# id :integer not null, primary key
# description :string(255)
# product_id :string(255)
# created_at :datetime
# updated_at :datetime
# price :string(255)
#
class Sale < ActiveRecord::Base
belongs_to :product
end
app/models/prodcuts.rb:
# == Schema Information
#
# Table name: products
#
# id :integer not null, primary key
# name :string(255)
# price :string(255)
# created_at :datetime
# updated_at :datetime
#
class Product < ActiveRecord::Base
has_many :sales
end
app/views/sales/do_stuff.html.erb:
<h1>Sales#do_stuff</h1>
<p>Find me in app/views/sales/do_stuff.html.erb</p>
<%= form_for(@sale) do |f| %>
<div><%= f.label(:product_id, "Product") %></div>
<div><%= f.collection_select(:product_id,
@products,
:id,
:name,
{prompt: true} ) %>
</div>
<div><%= f.label :price %></div>
<div><%= f.text_field :price %></div>
<div><%= f.submit "Submit" %></div>
<% end %>
Prototype:
Note the change to the javascript_include_tag--it will link to the file
public/javascripts/rails.js, which is the prototype file that rails
generates automatically:
app/views/layouts/application.html.erb
<!DOCTYPE html>
<html>
<head>
<title><%= @title %></title>
<%= csrf_meta_tag %>
<% javascript_include_tag(
:defaults
)
%>
<script type=''text/javascript'' >
document.observe("dom:loaded", function(){
function my_handler(event){
new Ajax.Request(''get_price'',
{
method: ''get'',
parameters: {product_id: $F(this)},
onSuccess: function(transport){
$(''sale_price'').setValue(transport.responseText)
},
onFailure: function(){ alert(''Something went
wrong...'')}
}
);
}
$(''sale_product_id'').observe(''change'',
my_handler);
});
</script>
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.