Jquery and ajax query in rails 3

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:

<%= f.label :product_id, "Product" %> <%= f.collection_select( :product_id, Product.all, :id, :name, options={} ) %>
<%= f.label :price, "Price" %> <%= f.text_field :price %>

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

Hope you can help me, thanks

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?

On Sep 8, 3:27am, 7stud – [email protected] 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!!.

Can you post your form tag? And your associations? I don’t
understand your
collection_select at all. Of course, I find trying to understand
collection_select to be harder than learning C++ to an intermediate
level. In fact, if I had 10,000 's, it would be faster for me
to write them out by hand than trying to figure out what arguments
collection_select takes.

jQuery:

No gem needed. Copy the rails.js file here:

and put it in the public/javascripts directory. Name the file
jquery.rails.js. Note how the javascript_include_tag below links to
that file as well as google:

app/views/layouts/application.htm.erb:

<%= @title %> <%= csrf_meta_tag %> <%= javascript_include_tag( "http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js", "jquery.rails.js" ) %>


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:

Sales#do_stuff

Find me in app/views/sales/do_stuff.html.erb

<%= form_for(@sale) do |f| %>

<%= f.label(:product_id, "Product") %>
<%= f.collection_select(:product_id, @products, :id, :name, {prompt: true} ) %>
<%= f.label :price %>
<%= f.text_field :price %>
<%= f.submit "Submit" %>
<% 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

<%= @title %> <%= csrf_meta_tag %> <%= javascript_include_tag( :defaults ) %>