Hi,

I also want a dynamic select menu but I want use jQuery and in my case
it doesn't works (I follow a different sly verano's tutorial) follow all
my steps

NOTE: I want use a list of data from a database

HOW CREATE the MODELS

$ rails generate model basket shape:string
$ rails generate model apple color:string shape_id:integer
$ rails generate model table basket:string apple:string

populate the database with a migration

$ rails generate migration create_hierarchy

/db/20120306193843_create_hierarchy.rb

class CreateHierarchy< ActiveRecord::Migration
  def self.up
    g1 = Basket.create(:shape => "awesome")
    g2 = Basket.create(:shape => "normal")

    a1 = Apple.create(:color => "Green", :basket_id => g1.id)
    a2 = Apple.create(:color => "Reds", :basket_id => g1.id)
    a3 = Apple.create(:color => "White", :basket_id => g2.id)
    a4 = Apple.create(:color => "Purple", :basket_id => g2.id)

  end

  def self.down
# you can fill this in if you want.
  end
end

MODELS

I have 3 model @table, @basket, @apple

class Apple
    belongs_to :tables
    belongs_to: baskets
end

class  Basket
    belongs_to :tables
    has_many :apples
end

class Table
    has_many :baskets
    has_many :apples
end

CONTROLLER

in the @table controller I have

def index
    @basket  = Basket.find(:all)
    @apple = Apple.find(:all)
    @table = Table.new
  end

VIEWS

the form is in app/views/tables/index.html.erb file

<%= form_for @table do |f| %>
<%= f.collection_select(:basket, @basket, :id, :shape, {:prompt =>true})
%>
<%= f.collection_select(:apple, @apple, :id, :color, {:prompt => true})
%>
<%= f.submit %>
<% end %>

IN THE ROUTES

root :to => 'table#index'

NOW when I load the page

http://localhost:3000

I have 2 list menu

{basket}

{apple}

**** jQuery ******

I use Rails 3.1 and this is my Gemfile I have

/////////////////////

source 'http://rubygems.org'

gem 'rails', '3.1.0'

gem 'sqlite3'

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails', "  ~> 3.1.0"
  gem 'coffee-rails', "~> 3.1.0"
  gem 'uglifier'
end

gem 'jquery-rails'

group :test do
  # Pretty printed test output
  gem 'turn', :require => false
end
//////////////

in the app/assest/javascript/table.js.coffee I have

jQuery ->
  apple = $('#table_apple').html()
  console.log(apple)
  $('#table_basket').change ->
    basket = $('#table_basket :selected').text()
    options = $(apple).filter("[label=#{basket}]").html()
    console.log(options)
    if options
      $('#table_apple').html(options)
    else
      $('#table_apple').empty()

but this script doesn't work 'cause onchange the first one {basket} the
second one is empty!

I don't know where's my mistake

thanks a lot in advance,

C

-- 
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@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to