Guys help me please. How to convert this rails-cast to rails 3 ? I'm
trying to convert it but still there is no luck.

http://railscasts.com/episodes/88-dynamic-select-menus

Thanks in Advanced. c",)

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

Search Discussions

  • Walter Lee Davis at Feb 8, 2012 at 5:12 pm

    On Feb 6, 2012, at 1:41 AM, sly verano wrote:

    Guys help me please. How to convert this rails-cast to rails 3 ? I'm
    trying to convert it but still there is no luck.

    http://railscasts.com/episodes/88-dynamic-select-menus

    Thanks in Advanced. c",)
    Are you using jQuery or Prototype in the rest of your app? The code on this 'cast is Prototype, so the only differences you will need to cover are in that realm. Everything else should just work.

    Walter

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Cluter Vipic at Mar 27, 2012 at 1:26 am
    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+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Walter Lee Davis at Mar 27, 2012 at 2:46 am

    On Mar 26, 2012, at 9:26 PM, Cluter Vipic wrote:

    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
    You cannot update the contents of a select using html() (which is similar to Prototype's update() function). The only way to alter the options of a select cross-browser is to do the following:

    1. Make a structure of your new elements, something like:

    var newOptions = [['one','One'],['two', 'Two'],['three', 'Three']]

    2. Remove all of the existing options:

    myPicker.options.length = 0;

    3. Loop through your array of new options, and build them up one at a time into options on the now-empty picker:

    for(i = 0; i < newOptions.length; i++){
    myPicker.options[myPicker.options.length] = new Option(newOptions[i][0], newOptions[i][1];
    }

    Hope this helps,

    Walter
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Bluetwin at Mar 27, 2012 at 4:43 pm
    What exactly aren't you having luck with? Do you need help with the
    JQuery conversion?
    As Walter Lee said, Rails 3 defaults with JQuery now.
    On Feb 6, 1:41 am, sly verano wrote:
    Guys help me please. How to convert this rails-cast to rails 3 ? I'm
    trying to convert it but still there is no luck.

    http://railscasts.com/episodes/88-dynamic-select-menus

    Thanks in Advanced. c",)
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Walter Lee Davis at Mar 27, 2012 at 4:47 pm

    On Mar 27, 2012, at 12:42 PM, bluetwin wrote:

    What exactly aren't you having luck with? Do you need help with the
    JQuery conversion?
    It sounds like he's using html() to replace the innards of a select list, and that doesn't usually work.

    Walter
    As Walter Lee said, Rails 3 defaults with JQuery now.
    On Feb 6, 1:41 am, sly verano wrote:
    Guys help me please. How to convert this rails-cast to rails 3 ? I'm
    trying to convert it but still there is no luck.

    http://railscasts.com/episodes/88-dynamic-select-menus

    Thanks in Advanced. c",)
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Cluter Vipic at Mar 28, 2012 at 12:46 am
    Thanks for the answers,

    follow the source web page, I have:

    <label for="table_basket">Basket</label>
    <select id="table_basket" name="table[basket]"><option value="">Please
    select</option>
    <option value="1">Awesome</option>
    <option value="2">Normal</option></select>

    <label for="table_apple">Apple</label>
    <select id="table_apple" name="table[apple]"><option value="">Please
    select</option>
    <option value="1">Green</option>
    <option value="1">Red</option>
    <option value="2">Yellow</option>
    <option value="2">White</option></select>

    <input name="commit" type="submit" value="Create Table" />

    To works I have to pass the right variable in the follow line

    options = $(apple).filter("[label=#{basket}]").html()

    I think the problem is here: label=#{basket}] that variable gone be 1 o
    2 (check out the option=value in <select id="table_apple"
    name="table[apple]"> menu) this value is function of the Basket choice

    well I think its only this the problem in the
    app/assest/javascript/table.js.coffee file but I don't know if it's only
    a
    simple syntax error

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

    thanks a lot anyway

    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+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Walter Lee Davis at Mar 28, 2012 at 1:47 am

    On Mar 27, 2012, at 8:46 PM, Cluter Vipic wrote:

    Thanks for the answers,

    follow the source web page, I have:

    <label for="table_basket">Basket</label>
    <select id="table_basket" name="table[basket]"><option value="">Please
    select</option>
    <option value="1">Awesome</option>
    <option value="2">Normal</option></select>

    <label for="table_apple">Apple</label>
    <select id="table_apple" name="table[apple]"><option value="">Please
    select</option>
    <option value="1">Green</option>
    <option value="1">Red</option>
    <option value="2">Yellow</option>
    <option value="2">White</option></select>

    <input name="commit" type="submit" value="Create Table" />

    To works I have to pass the right variable in the follow line

    options = $(apple).filter("[label=#{basket}]").html()

    I think the problem is here: label=#{basket}] that variable gone be 1 o
    2 (check out the option=value in <select id="table_apple"
    name="table[apple]"> menu) this value is function of the Basket choice

    well I think its only this the problem in the
    app/assest/javascript/table.js.coffee file but I don't know if it's only
    a
    simple syntax error

    jQuery ->
    apple = $('#table_apple').html()
    $('#table_basket').change ->
    basket = $('#table_basket :selected').text()
    options = $(apple).filter("[label=#{basket}]").html()
    if options
    $('#table_apple').html(options)
    You can't just do this, not unless behind the scenes, jQuery is imputing that the array is an array of select options, and that the target is a select, and is magic-ly and silently doing the Right Thing™ and building new Option objects and inserting them in the select. Really, I'm not kidding here, this is not going to work in any or many browsers.

    I would love to be wrong about this, but as far as I know, you MUST:

    1. Zero the length of the select object.
    2. One at a time, create a new Option object, assign it the desired value and text, and add it to the select's options array.

    After you have done those things, in that order, you will have a working select with the new options.

    I know it doesn't look cool to have that old-school bare JavaScript hanging out in your CoffeeScript, but really, needs must.
    else
    $('#table_apple').empty()

    thanks a lot anyway
    You're welcome,

    Walter
    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+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Cluter Vipic at Apr 18, 2012 at 1:55 am
    thanks for your answer

    even if I don't speak a perfect english I really appreciate your answer
    only I hope to understand your effort

    thanks a lot again

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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedFeb 6, '12 at 6:42a
activeApr 18, '12 at 1:55a
posts9
users4
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2022 Grokbase