Ruby on Rails Auto Select

Hello Guys,

Once Again I m back with My another Useful Article for You. In this Article I will describe you, how to auto select more than two select box. So, are U Ready??

Update: If you wish to implement the same functionality using Rails 3, Jquery and Rails3-UJS, please follow: Jquery on Rails 3 Autoselect.

Here is the Task that U have to achieve :-

You have three Select Boxes one for States, second for city and third for college.

What You want to Do :-

You want that if Some one selects State for Example :- Karnataka, the next drop down will automatically show u the lists of cities that comes Under Karnataka, Furthermore if you select Say Bangalore, the third drop down will show You the lists of colleges that comes under that city.

In short:-

STATE -> CITY -> COLLEGE

KARNATAKA -> BANGALORE -> IIT,Bangalore

Ruby on Rails with Ajax makes it very easy to achieve, It’s a 5 Step Process, Let’s See How

Step 1: create a new application, I am naming my application here ‘sample’

Go to konsole/command Prompt:

:    rails sample

This will create particular set of directories and files. Go into that Application

:    cd /sample

Make a Model ‘State’

:    ruby script/generate model State

This will create some files into your application, now open your migration file in your editor

i.e    fever/db/migrate/001_create_states.rb

Type

:    t.column :state, :string

and then save it.

open the model i.e state.rb in some editor

i.e    fever/app/model/state.rb

Type

:    has_many :cities

save this also, now Run

:    rake db:migrate

This will migrate your states table in your database. To save your time I am generating scaffold here, go to konsole/command prompt and type

:    ruby script/generate scaffold State

Note: this will create controller named states_controller.rb in your app/controllers directory.

Your first step is done, now we will proceed on 2nd step.

Step 2: create another model with the name city, for that type :

:    ruby script/generate model City

This will create some files into your application, now open your second migration file in your editor

i.e    fever/db/migrate/002_create_cities.rb

Type

:    t.column :city, :string

t.column :state_id, :integer

save it.

open the model i.e city.rb in some editor

i.e    fever/app/model/city.rb

Type

:    belongs_to :state

has_many :colleges

save this and run

:    rake db:migrate

This will migrate your cities table in your database. Create scaffold of this, Type

:    ruby script/generate scaffold City

Note: this will create another controller named cities_controller.rb in your app/controllers directory.

Your Second step is done, now we will proceed on 3rd step.

Step 3: Create third model with the name college, for that type :

:    ruby script/generate model College

This will create some files into your application, now open your Third migration file in your editor

i.e    fever/db/migrate/003_create_colleges.rb

Type

:    t.column :college, :string

t.column :city_id, :integer

save it.

open the model i.e college.rb in some editor

i.e    fever/app/model/college.rb

Type

:    belongs_to :city

save this and run

:    rake db:migrate

This will migrate your colleges table in your database. Create scaffold of this, Type

:    ruby script/generate scaffold College

Note: this will create another controller named colleges_controller.rb in your app/controllers directory.

You have done with your 3rd Step. Now we will move onto our fourth step

Step4: Create a Third Model with name = User, where we will display all the action, for that type :

:    ruby script/generate model User

This will create some files into your application, now open your Fourth migration file in your editor

i.e    fever/db/migrate/004_create_users.rb

Type

:    t.column :firstname, :string

t.column :lastname, :integer

save it(I think two fields are more than enough to solve my purpose here).

Create a scaffold of this model

:    ruby script/generate scaffold User

Note: that’s it. You have done with your basics steps, now we will use Ajax here, but before that I want you to insert some values of states, cities and colleges manually, be sure that you are giving right ‘state_id’, and ‘city_id’ in your corresponding ‘cities’ and ‘colleges’ tables

Step 5: Open app/views/users/new.rhtml

and type below line between your head section

:    <HEAD>

<%= javascript_include_tag “prototype” %>

</HEAD>

Open app/views/users/_form.rhtml, and type the below code after your firstname and lastname fields

:    <p>State <br />

<% @states = State.find(:all) %>

<select name=”state_id” id=”state_id”>

<option value=””>Select City</option>

<% @states.each do |state| %>

<option value=”<%= state.id %>”>

<%= state.state %>

</option>

<% end %>

</select>

</p>

<p>City

<div id=”city_id_container”>

<select name=”city_id” disabled=”disabled”>

<option value=””>No City</option>

</select>

</div>

<%= observe_field(“state_id”, :frequency => 0.25, :update => “city_id_container”, :url => { :action => :add_link_city }, :with => “‘state_id=’+value”) %>

</p>

Now open your users controller i.e app/controllers/users_controller.rb, and write the code as shown below

: def add_link_city

@cities = City.find_all_by_state_id(params[“state_id”])

end

Now create a new file in app/views/users/add_link_city.rhtml, and add the code as shown

: <HTML>

<HEAD>

<%= javascript_include_tag “prototype” %>

</HEAD>

<BODY>

<p>    <% @html = “<select id=’city_id’ name=’city_id’>” %>

<%= @html += “<option value=”>No City</option>” %>

<% @cities.each do |@cities| %>

<%= @html = “<option value=’#{@cities.id}’>#{@cities.city}</option>” %>

<% end %>

<%= @html = “</select>” %>

</p>

<p>College

<div id=”college_id_container”>

<select name=”college_id” disabled=”disabled”>

<option value=””>No College</option>

</select>

</div>

<%= observe_field(“city_id”, :frequency => 0.25, :update => “college_id_container”, :url => { :action => :add_link_college }, :with => “‘city_id=’+value”) %></p>

</BODY>

</HTML>

Go back again in your app/controllers/users_controller.rb, and add the method as shown below

: def add_link_college

@colleges = College.find_all_by_city_id(params[“city_id”])

#@cities = City.find(:first, :conditions => [“city = ?”, params[:city_id]])

end

Now create a new file in app/views/users/add_link_college and add the following code

: <HTML>

<HEAD>

<%= javascript_include_tag “prototype” %>

</HEAD>

<BODY>

<p>    <% @html = “<select id=’college_id’ name=’college_id’>” %>

<%= @html += “<option value=”>No College</option>” %>

<% @colleges.each do |@colleges| %>

<%= @html = “<option value=’#{@colleges.id}’>#{@colleges.college}    </option>” %>

<% end %>

<%= @html = “</select>” %>

</p>

<p> If Your College is not on the List, Please Click <%= link_to     “Here”, :controller => ‘colleges’, :action => ‘new’, :id =>

@colleges.city_id %> here to Add Your College</p>

<p>Course

<div id=”course_id_container”>

<select name=”course_id” disabled=”disabled”>

<option value=””>No Course</option>

</select>

</div>

<%= observe_field(“college_id”, :frequency => 0.25, :update =>

“course_id_container”, :url => { :action => :add_link_course },

:with => “‘college_id=’+value”) %></p>

</BODY>

</HTML>

That’s It, You have done. Now you can check this in Your Browser. Type in Your url, http://localhost:3000/users/new, and see the magick.

Waiting for Your Valuable Responses.

Advertisements

23 thoughts on “Ruby on Rails Auto Select

  1. Hi Puneet,

    It is such a Great Article, and I am using this in My Application. It is a great effort from your side. Please Keep on Posting these Kinds of articles, It would help others as well….

    Waiting for your next article to be published. Hope that would also interesting.

    Thanks once again Puneet

  2. Why are you putting the data aspect of the app, in the template view ?
    This should go into the controller.
    : State

    Select City

    <option value=””>

  3. SyntaxError in Users#new am getting a syntax error……………
    SyntaxError in Users#new

    Showing app/views/users/new.rhtml where line #2 raised:

    compile error
    ./script/../config/../app/views/users/new.rhtml:2: Invalid char `\223′ in expression
    ./script/../config/../app/views/users/new.rhtml:2: Invalid char `\224′ in expression

    Extracted source (around line #2):

    1:
    2:
    3:
    4: New user
    5:

    Trace of template inclusion: /app/views/users/new.rhtml

    RAILS_ROOT: ./script/../config/..
    Application Trace | Framework Trace | Full Trace

    #{RAILS_ROOT}/app/views/users/new.rhtml:13:in `compile_template’

    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/base.rb:319:in `compile_and_render_template’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/base.rb:301:in `render_template’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/base.rb:260:in `render_file’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:806:in `render_file’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:711:in `render_with_no_layout’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/layout.rb:247:in `render_without_benchmark’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/benchmarking.rb:50:in `render’
    C:/ronr/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/benchmarking.rb:50:in `render’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:1096:in `perform_action_without_filters’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:632:in `call_filter’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:634:in `call_filter’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:619:in `perform_action_without_benchmark’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/benchmarking.rb:66:in `perform_action_without_rescue’
    C:/ronr/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/benchmarking.rb:66:in `perform_action_without_rescue’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/rescue.rb:83:in `perform_action’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:430:in `send’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:430:in `process_without_filters’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:624:in `process_without_session_management_support’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/session_management.rb:114:in `process’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:330:in `process’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/dispatcher.rb:41:in `dispatch’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/rails.rb:78:in `process’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/rails.rb:76:in `synchronize’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/rails.rb:76:in `process’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:618:in `process_client’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:617:in `each’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:617:in `process_client’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:736:in `run’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:736:in `initialize’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:736:in `new’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:736:in `run’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:720:in `initialize’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:720:in `new’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:720:in `run’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/configurator.rb:271:in `run’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/configurator.rb:270:in `each’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/configurator.rb:270:in `run’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/bin/mongrel_rails:127:in `run’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/command.rb:211:in `run’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/bin/mongrel_rails:243
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:488:in `load’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:488:in `load’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:342:in `new_constants_in’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:488:in `load’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/servers/mongrel.rb:60
    C:/ronr/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require’
    C:/ronr/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:495:in `require’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:342:in `new_constants_in’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:495:in `require’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/server.rb:39
    C:/ronr/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require’
    C:/ronr/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’
    script/server:3

    #{RAILS_ROOT}/app/views/users/new.rhtml:13:in `compile_template’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/base.rb:319:in `compile_and_render_template’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/base.rb:301:in `render_template’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/base.rb:260:in `render_file’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:806:in `render_file’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:711:in `render_with_no_layout’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/layout.rb:247:in `render_without_benchmark’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/benchmarking.rb:50:in `render’
    C:/ronr/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/benchmarking.rb:50:in `render’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:1096:in `perform_action_without_filters’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:632:in `call_filter’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:634:in `call_filter’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:619:in `perform_action_without_benchmark’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/benchmarking.rb:66:in `perform_action_without_rescue’
    C:/ronr/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/benchmarking.rb:66:in `perform_action_without_rescue’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/rescue.rb:83:in `perform_action’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:430:in `send’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:430:in `process_without_filters’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:624:in `process_without_session_management_support’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/session_management.rb:114:in `process’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:330:in `process’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/dispatcher.rb:41:in `dispatch’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/rails.rb:78:in `process’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/rails.rb:76:in `synchronize’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/rails.rb:76:in `process’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:618:in `process_client’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:617:in `each’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:617:in `process_client’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:736:in `run’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:736:in `initialize’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:736:in `new’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:736:in `run’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:720:in `initialize’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:720:in `new’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:720:in `run’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/configurator.rb:271:in `run’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/configurator.rb:270:in `each’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/configurator.rb:270:in `run’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/bin/mongrel_rails:127:in `run’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/command.rb:211:in `run’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/bin/mongrel_rails:243
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:488:in `load’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:488:in `load’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:342:in `new_constants_in’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:488:in `load’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/servers/mongrel.rb:60
    C:/ronr/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require’
    C:/ronr/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:495:in `require’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:342:in `new_constants_in’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:495:in `require’
    C:/ronr/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/server.rb:39
    C:/ronr/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require’
    C:/ronr/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’
    script/server:3

    Request

    Parameters: None

    Show session dump


    flash: !map:ActionController::Flash::FlashHash {}

    Response
    Headers: {“cookie”=>[], “Cache-Control”=>”no-cache”}

  4. Will you please show me what exactly your line no. 2 in new.html.erb is? are u using javascript_include_tag “prototype”, please if this is the error as i can see line no. 2 in my blog please fix it like this :

    Please remember the double quotes you are giving. please let me know if this works

  5. I’m getting this error in the cascading dropdown–
    (using Ruby version 1.8.6 (i686-darwin8.10.1)
    RubyGems version 1.2.0 Rails version 1.2.5)
    I can pull up /users and /show… but in /edit or /new I get the error. what to do? thanks…
    **************************
    NameError in Users#new

    Showing app/views/users/_form.rhtml where line #30 raised:

    undefined local variable or method `“state_id”’ for #<#:0x32ab810>
    Extracted source (around line #30):

    27:
    28:
    29:
    30: 0.25, :update => “city_id_container”, :url => { :action => :add_link_city }, :with => “’state_id=’+value”) %>
    31:

  6. I think I got the problem fixed. I changed the double quotes to single quotes (different from the blog) in app/views/users/_form.rhtml to look like — ‘state_id’ and ‘city_id_container’ — then restarted everything and it seemed to work.
    Thank you for the cascading drop down code, I’d been looking for this.

  7. Pingback: Jquery on Rails3 auto select : : puneetpandey.com

  8. Pingback: JQUERY ON RAILS3 AUTO SELECT « Puneet Pandey's Weblog

  9. Bob Smith

    I am trying to do something similar to this in my app and am wondering if you know a solution.

    In your app, would the observe_field calls work if the city or state name was duplicated? I am talking about sometown,mass and sometown,utah. I’m trying to code a way to prevent duplicates in the user list. A duplcate for me would be 2 records with the same address and same zip code.

    So far when observe_field is called, I can’t get it to pass references to the new address and new zip to use for a search. Everything works fine using only the address, but recently I had 2 people with an address of 201 Main St with different zip codes.

    Thanks

    Bob

    1. Hi Bob,

      I am little bit confused in understanding your requirement!! But to give you the holistic view – yes, the observe_field call works if the city or state name is duplicated and you can prevent duplicate records by passing some additional parameters based on the value selected.

      However, if you can post some code snippet here, I would be glad to assist you more.

      – Puneet

  10. Ganeshkumar.S

    1. > ruby script/generate model State
    # Its create model State

    2. > ruby script/generate scaffold State
    # Its also create model(State) ,controller and view .That time its caused the error.

    Error :

    > rails generate scaffold State
    invoke active_record
    Another migration is already named create_states: /Users/eit/cname/db/migrate /20130703035518_create_states.rb

    1. If you go by scaffold, then you don’t need to run the generate model command again. Now that you’ve done this, below are the steps you can execute:

      – Remove all the files generated by model State command.
      – run command for scaffold
      – If you’ve ran the rails g model State command and now trying to execute scaffold command, generator will ask you to overwrite the files. When it says so, simply pass “Y”

      Let me know if those steps are of any help.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s