SitePoint Sponsor

User Tag List

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

    check if method = get or post?

    Hi,

    I'm trying to apply DRY concept so when I want to display an product for example, I have /article/show/2 using the id to retrieve data. But if I want to use the same action with a reference field instead of id, what should I do?

    I'm using a form, so I display the ref in the select box sending the id
    PHP Code:
    <%= start_form_tag :controller => 'product', :action => 'show' %>
        <
    p>Ref.:<%= collection_select("product""id", @references"id""reference") %>
    <%= 
    submit_tag "search" %></p
    the source page seems ok too
    PHP Code:
    <option value="11">IPA235</option
    but it doesn't work, I still get Couldn't find Product without an ID

    Did I forget something?
    Last edited by HenriIV; Jul 25, 2006 at 03:47.

  2. #2
    SitePoint Zealot HenriIV's Avatar
    Join Date
    Jun 2004
    Location
    France
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I should have asked differently:

    in order to work, my action must look like this
    PHP Code:
      def show
        
    @hotel Hotel.find(params[:hotel][:id])
      
    end 
    because post sends data in an hash

    but when I use get method to access the same page, I must use
    PHP Code:
      def show
        
    @hotel Hotel.find(params[:id])
      
    end 
    I would like to avoid to add a different template / action to do exactly the same thing. Is there a smart way to do this?

  3. #3
    SitePoint Zealot HenriIV's Avatar
    Join Date
    Jun 2004
    Location
    France
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I fixed it doing something like that:
    PHP Code:
    if params[:hotel].nil?
          @
    id params[:id]
    else
          @
    id params[:hotel][:id]
    end 
    is there a better way to do this?

  4. #4
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Slightly more concise:

    Code:
    @id = params[:hotel] ? params[:hotel][:id] : params[:id]


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
  •