SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    a form tutorial?

    In sitepoint's rails book there isn't a full explanation of how forms work. Only a form with text fields to create "Stories" and nothing about select boxes, checkboxes etc. Is there a good tutorial about that?

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    These operate on activerecord objects:

    http://www.railsapi.org/actionview-h...shelper-select
    http://www.railsapi.org/check_box

    Related methods on the right are very useful!

    For lower level helpers that just generate html (ie non activerecord), append _tag to the name: check_box => check_box_tag, select => select_tag:

    http://www.railsapi.org/select_tag
    http://www.railsapi.org/check_box_tag

    A better way to do forms in Rails is form_for. Normally you pass the activerecord object to every helper. With form_for you pass the activerecord object to the form once, so you don't have to pass it to every helper.

    http://www.railsapi.org/form_for

  3. #3
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you railsapi.org is much better than read api.rubyonrails.com (much more difficult to grasp). Anyway i wonder if in the example of the "select" helper it is better to move the "find method" in the "choices" select method parameter to the controller and put in the view with the helper only the result from an instance variable in the controller.

    Another question is: if i have to create a select box only to choose from a list of "users" can i omit the object an method parameter form the select method?
    In essence i have to write a form with only a select box to choose from a list of users in a database and to send the form with the id of the choosed user and then with that id back in the same controller action i have to associate that user with another model.

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The method is find_all in this example, often you want to find with :conditions (only users older than 18 for example). I always put these database finders in the model (Person.find_grown_ups for example). Then you assign this to an instance variable in the controllor (@people = Person.find_grown_ups). Then you create this array of @people.map{|p| [p.name, p.id]} in the view (because it's logic for a view helper).

    If you don't want to save the data entered in the select box in the db then you use the _tag variants (select_tag).

  5. #5
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2 View Post
    The method is find_all in this example, often you want to find with :conditions (only users older than 18 for example). I always put these database finders in the model (Person.find_grown_ups for example). Then you assign this to an instance variable in the controllor (@people = Person.find_grown_ups). Then you create this array of @people.map{|p| [p.name, p.id]} in the view (because it's logic for a view helper).

    If you don't want to save the data entered in the select box in the db then you use the _tag variants (select_tag).
    i have to create automatically a selet box with a list of "Users" in the database. When we select a "User" from the list of the select box we send the form with the "id" of that choosed "User". Which is the best method?


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
  •