hi people
I don''t know much about ajax - jquery. And right now I need to use
some functionality with them.
In a form (sales model) I have the following code:
<div>
<%= f.label :product_id, "Product" %>
<%= f.collection_select( :product_id, Product.all, :id, :name,
options={} ) %>
</div>
<div>
<%= f.label :price, "Price" %>
<%= f.text_field :price %>
</div>
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.
I tried this example but didn''t work
http://www.webmonkey.com/2010/02/building_with_ajax_and_rails/
Hope you can help me, thanks
--
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.
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? -- 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.
On Sep 8, 3:27 am, 7stud -- <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Why would you think prototype code would work in a jquery app?I tried the example in an app with prototype (not jquery), but it didn''t work, that''s the reason I''m looking for a jquery way>What version of rails are you using?Version 3.0.9> What have you done to setup your app for jquery?I''ve installed jquery-rails gem> Why did you think that information was unnecessary?I didn''t, I''ve made a mistake, sorry>Aren''t you aware that rails changes every week?I know rails changes, but I didn''t know it changes that often!!. -- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Can you post the your form tag? -- 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.
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.