SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2008
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Brand New To RoR: Radio buttons and other form fields

    Hello I just started messing around with RoR yesterday. I have a simple little data entry app I'm learning on currently. I just set it up with the scaffold command and then used rake to make the db for me.

    I have the fields all set up as "string" so they all by default from the scaffold use this same text boxes in the new.html.erb file
    Code:
    <% form_for(@beer) do |f| %>
      <p>
        <b>Name</b><br />
        <%= f.text_field :name %>
      </p>
    My question is what is the do |f| actually doing. What is |f| referring to? Also how can I use a grouping of radio buttons to submit information as well as a select box with set options? (I want to have a "yes", "no", and "maybe") radio group for one of my fields and I want a select box with the US States in it.

    I have looked up the form stuff and all i could get it to do was display a radio button that did nothing when submitted. - I think this is probably my lack of understanding of 'f.'

    I know it has to be a simple little thing and I look like a real noob for asking - but hey, I am a noob! I'm really excited about learning more RoR as I go on - its really fun so far.

    Thanks!

  2. #2
    SitePoint Zealot Xavius's Avatar
    Join Date
    Sep 2005
    Location
    Toronto, Canada
    Posts
    195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    f refers to an element of form_for - you could have just as easily used d, x or anything else.

    When you submit a form each form field is stored in a nested hash. In your example to access the value of :name you would have had to do params[:beer][:name] (beer being the name of the hash that holds your beer form and name being the hash that stores your name value).

    The following ruby code you may also find useful:
    Code Ruby:
    1.upto(9) do |number|
      puts number
    end

    In this example, number starts off with the value 1, then 2, then 3 and all the way up to 9. Note that number could have been replaced with x or anything else.


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
  •