SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How change text_field to select_tag?

    Hello all, this is my first post in ruby forums.
    Im starting to learn ruby language.

    I've made this form

    Code:
    <!--[form:contract]-->
    <p><label for="contract_total">Total</label><br/>
    <&#37;= text_field 'contract', 'total'  %></p>
    
    <p><label for="contract_arrival">Arrival</label><br/>
    <%= date_select 'contract', 'arrival'  %></p>
    
    <p><label for="contract_departure">Departure</label><br/>
    <%= date_select 'contract', 'departure'  %></p>
    
    <p><label for="contract_adults">Adults</label><br/>
    <%= text_field 'contract', 'adults'  %></p>
    
    <p><label for="contract_children">Children</label><br/>
    <%= text_field 'contract', 'children'  %></p>
    
    <p><label for="contract_pending">Pending</label><br/>
    <%= text_field 'contract', 'pending'  %> Pending indicates if total or minimum amount has been payed.<br>
    <b>0</b>= total amount payed.<br>
    <b>1</b>= minimum amount payed.</p>
    <p><label for="contract_children">Un/Confirmed</label><br/>
    <%= text_field 'contract', 'unconfirmed'  %></p> Confirmed indicates if customer has payed some.<br>
    <b>0</b>= Yes, customer has payed.<br>
    <b>1</b>= No, customer has not payed nothing.</p>
    <!--[eoform:contract]-->
    I dont want users can write into text fields, i would need they can select a numer of list (1-15). I've been reading about it and maybe i could use select_tag, but i dont know how change text_field to a "combo_box"?

    How could i do it?

    Thanks in advance.
    R3g@rd$

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Miami, FL
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You mean, like, select(object, method, choices, options = {}, html_options = {})
    http://shownd.com - Your free online design portfolio.
    http://rehashclothes.com - Swap your clothes.
    http://orpheuxdesign.com - My design studio.

  3. #3
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As realmadrid2727 indicates, changing to a select drop down box is quite easy, and the information about the syntax of the 'select' method is available on the RoR api page. Have a look here:
    http://api.rubyonrails.com/classes/A...r.html#M001752

    The problem is where do you get your list/array of options!

    One way is to create a model to contain the items being selected and store them in the database. This is great if the list is large or being changed a lot. For example a list of products.

    However, if the list is small and is unlikely to change (or you don't want it to change) I think there is a better way: define it in your model.

    For example, in your set up I'd do this:

    In the contract model I'd add:
    Code:
    NUMBERS_OF_PEOPLE = (0..15).to_a
    
    def self.numbers_of_people
      NUMBERS_OF_PEOPLE
    end
    In the controller method I'd add
    Code:
      @numbers_of_people = Contract.numbers_of_people
    Then in the form partial:
    Code:
    <p><label for="contract_adults">Adults</label><br/>
    <&#37;= select 'contract', 'adults', @numbers_of_people.collect {|p| p,p} -%></p>
    
    <p><label for="contract_children">Children</label><br/>
    <%= select 'contract', 'children', @numbers_of_people.collect {|p| p,p}  %></p>
    The advantages of doing it this way is that there are:
    • a single easy to find place where the numbers can be changed. You change it in one place and the whole application is updated automatically.
    • The array of available options is made available to other input systems. All they have to do is look-up Contract.numbers_of_people.
    • It makes the array of available options accessible to the model and therefore they can be used easily in model methods such as Contract.numbers_of_people:


    In the Contract model:
    Code:
      validates_inclusion_of :children, :in => NUMBERS_OF_PEOPLE
      validates_inclusion_of :adults, :in => NUMBERS_OF_PEOPLE


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •