SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    RoR Sorting/Forms Questions

    Hey guys,

    I'm making an application that gets information stored from a database and outputs it in tables. I got the basic part down fine, but now I want to sort it using form elements. This is where I hit a wall:

    I created the form starter in ruby code "<%= form_tag :action=>"sort" %>" and was wondering if this would post to a method in the controller called "sort"? If so, I would just do something like "if request.post? ... end"? Also, if I am trying to make the comparisons with database entries, what is the best way to do it?

    Something like the following?:

    @items = Item.find(Item.style == :form_style)

    Is the :form_style automatically taken from the form if I had, say, a selectbox named "form_style" and compare it to each Item with the same style entry in the table?

    Are there any tutorials you guys know of on how to do this?

    I'm also looking at how to paginate if anyone knows how to do that?

    Any help would be greatly appreciated... thanks!!!

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have to use params[:form_field_name] to get form data:

    Code ruby:
    @items = Item.find_by_style(params[:style])

    Rails provides find_by_xxx methods for every column in your database.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2 View Post
    You have to use params[:form_field_name] to get form data:

    Code ruby:
    @items = Item.find_by_style(params[:style])

    Rails provides find_by_xxx methods for every column in your database.
    How do I query using multiple form elements? For example, if I have checkboxes that I need to compare to a database table to query the results set.

  4. #4
    l 0 l silver trophybronze trophy lo0ol's Avatar
    Join Date
    Aug 2002
    Location
    Palo Alto
    Posts
    5,329
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    As far as grabbing appropriate data from your database, you should take a look at ActiveRecord's docs. Specifically the section on conditions:

    Code Ruby:
    @items = Item.find(:all, :conditions => {:style => params[:style], :two => params[:two], :three => params[:three]})


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
  •