Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Rails:Populating Select boxes with model data

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 292
    Comment on it

    In model specific form we use select method to display a select box and populate it with model data.

    Example:

    #controller
    @location = Location.new()
    
    #view
    <%= select(:location,:city_id,City.all.collect {|p| [ p.name, p.id ] }) %>
    or
    <%= f.select(:city_id,City.all.collect {|p| [ p.name, p.id ] }) %>
    
    where f is the form object.
    

    select tag also takes optional arguments . They are :

    :multiple - When set to true we can select more than one option.

    :disabled - When set to true the select box will be disabled in the web page

    :include_blank - When set to true an empty option will be created

    :prompt - When a string is passed to this option it is displayed as option without any value in the web page.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: