SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Zealot HenriIV's Avatar
    Join Date
    Jun 2004
    Location
    France
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [Rails] multiple arguments

    Hi,

    I'm struggling to pass multiple arguments like that:

    PHP Code:
    @hotels Hotel.find_featured(params[:category], [:type], [:city_id]) 
    but I get an SQL error
    city_id = '--- :city_id\n' when an id is supposed to be passed
    same issue with the other params

    in the form view, I have something like that
    PHP Code:
    City: <%= collection_select("city""hotel[city_id]", @cities"id""name") %> 
    I missed something but what?
    Last edited by HenriIV; Apr 29, 2006 at 11:14.

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have to repeat params:

    Code:
    @hotels = Hotel.find_featured(params[:category], params[:type], params[:city_id])
    just [:city_id] means an array with one element: a symbol :city_id. params[:city_id] means that Ruby will look up :city_id in the params hash (or associative array, if you are coming from PHP).

    Or this:

    Code:
    @hotels = Hotel.find_featured(*params.values_at(:category, :type, :city_id))
    Ignore the second one if it confuses you ;-).

  3. #3
    SitePoint Zealot HenriIV's Avatar
    Join Date
    Jun 2004
    Location
    France
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, it's ok now, mysql doesn't complain anymore but I get the complete hash.
    I can't write params[:city[3]] to get the third parameter for example. do I need to extract the value in the find_featured method of hotel Class?

  4. #4
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by HenriIV
    thanks, it's ok now, mysql doesn't complain anymore but I get the complete hash.
    I can't write params[:city[3]] to get the third parameter for example. do I need to extract the value in the find_featured method of hotel Class?
    Depends. You could extract it before passing it to the find_featured method. That might be better so that find_featured is only worrying about the variable it wants not about a hash or array that it will need more knowledge about than it needs.
    Erh

  5. #5
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think params[:city[3]] should probably be params[:city][3]

  6. #6
    SitePoint Zealot HenriIV's Avatar
    Join Date
    Jun 2004
    Location
    France
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doesn't seem to work. I don't understand how Rails forms are working...

    If I have in my controller
    PHP Code:
    @hotels Hotel.find_featured(params[:category], params[:type], params[:city_id]) 
    and in my view
    PHP Code:
     City: <%= collection_select("city""hotel[city_id]", @cities"id""name") %> 
    I get a complete hash in my model like that
    '--- \n- city_id\n- \"4\"\n' I just try to get "4" in this case but can't extract it. Sometimes, Rails seems much more complicated than Java!

    If someone has a good tutorial about forms, I really need it

  7. #7
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know if this is causing it or not but the second parameter to collection_select should be the name of a method. So not "hotel[city_id]" but city_id and it's supposed to be a method of the object your passing in the first parameter.

    Check this out and see if it helps any.
    Erh

  8. #8
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are there 'categories' and 'types' tables, or are these just collums of the 'hotels' table?

    Is there a special reason to use a separate table for cities, and not for categories and types?

    It is a lot easier if you use the same method for categories, types and cities.

    Maybe this works:

    Code:
    City: <select name="city_id">
      <%= options_for_select(@cities.map{|c| [c.name, c.id]}) %>
    </select>
    The @hotels = ... code is still the same.

  9. #9
    SitePoint Zealot HenriIV's Avatar
    Join Date
    Jun 2004
    Location
    France
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    >Mandibal
    You're right but it doesn't change anything, it's just the id and name of the field
    The wiki page is the one I used to learn from but it's not clear to me this time

    >Fenrir2
    I could add cities directly in the hotel table but it's against DB normal forms
    I tried your code but it produces the same output.

    In the model, I added a default value but it doesn't work
    def self.find_query(category, type, city_id = 4, ...)

    so I tried
    def self.find_query(category, type, city_id, ...)
    city_id = 4
    ...

    and it does work so it's really a problem between values sent by the form to the model. There's something wrong in params but have no idea to extract the good value

  10. #10
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you get an error? You can check the params hash under the stack trace. What does it say?

    If you don't get an error, what does:

    Code:
    <%= debug @controller.params %>
    show you?

    And what if you do:

    Code:
    <input type="text" name="city_id" />
    and type in the id manually?

    EDIT:

    Wait, maybe this is the problem:

    city_id should be an integer. Try:

    Code:
    @hotels = Hotel.find_featured(params[:category], params[:type], params[:city_id].to_i)

  11. #11
    SitePoint Zealot HenriIV's Avatar
    Join Date
    Jun 2004
    Location
    France
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <%= debug @controller.params %>
    returns
    PHP Code:
    city: !map:HashWithIndifferentAccess 
      id
    "2" 
    but the content in the log file is:
    city_id = '--- :city_id\n'


    The .to_i returns an error:
    undefined method `to_i' for {"id"=>"2"}:HashWithIndifferentAccess

  12. #12
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You aren't using my code huh ;-):

    The problem is that city_id is a hash, and not the id you want. You can either make sure that city_id is the id:

    Code:
    City: <select name="city_id">
      <%= options_for_select(@cities.map{|c| [c.name, c.id]}) %>
    </select>
    Or you can get the id out of the hash:

    Code:
    @hotels = Hotel.find_featured(params[:category], params[:type], params[:city_id][:id])

  13. #13
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by HenriIV
    >Mandibal
    You're right but it doesn't change anything, it's just the id and name of the field
    The wiki page is the one I used to learn from but it's not clear to me this time
    Mandibal and Fenrir2 are right, try doing view source on the form field, it's probably not what you expect.

    From this:

    The .to_i returns an error:
    undefined method `to_i' for {"id"=>"2"}:HashWithIndifferentAccess
    You've probably got an input something like this:

    HTML Code:
    <input name="hotel[city_id][id]" .... />
    Douglas
    Hello World

  14. #14
    SitePoint Zealot HenriIV's Avatar
    Join Date
    Jun 2004
    Location
    France
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, it's ok now, thanks guys

    params[:city_id][:id] is working but I don't understand why params[:city_id][2] failed as it was supposed to extract the id from '--- \n- city_id\n- \"4\"\n' hash
    I really need to go deeper into Ruby


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
  •