Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4541536
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:15:11+00:00 2026-05-21T15:15:11+00:00

I’m currently trying to use link_to with a helper method that creates links for

  • 0

I’m currently trying to use link_to with a helper method that creates links for creating items dynamically, rather than using the new_object_path methods. Rails 3 allows for polymorphic URLs, which fl00r helped me identify and use in the form I was originally searching for. However, while using Rails’ polymorphic_url method as shown here does pass the argument in the http request, the related form field on the returned page isn’t filled in, and isn’t getting set. I’m trying to build an order_item from an order that also is built from a photo.

Does anyone see what’s going wrong here? Related code is below:

# the helper method
def add_cart_button_for(arr, attr_hash={})
  link_to button(:cart_add), polymorphic_url( [:new] + arr, attr_hash )
end

# the call in index.html.haml
= add_cart_button_for [current_order, :order_item], :photo_id => photo.id

# _form.html.haml for order_item
- form_for [ @order, @order_item ] do |f|
  %p
    = f.label :photo_id
    %br
    = f.text_field :photo_id

Here’s the original question:

I’m trying to create a link to create a [:new, @order, @order_item] where @order_item.photo_id can be passed in the link.

Now, I’m not sure if that makes a lot of sense, so I’ll try to clarify my problem here.

From what I’ve seen, Rails 3 has a newish syntax for linking to resource actions using an array structured as [:action, object] or for deeply nested resources, [:action, parent, child]. For this particular example, I’m using link_to [:new, @order, :order_item]. These buttons will be attached to each photo in a photo gallery.

Is it possible to pass the id of the photo as the attribute photo_id of the new order item?

So far I’ve tried link_to [:new, @order, :order_item], :photo_id => photo.id, but that doesn’t seem to work. I’ve also tried putting :photo_id in an explicit hash. Is there a way to follow this new syntax and achieve this functionality?

Update:
I’m really hoping to find something along the lines of:

 link_to "new photo", [:new, @order, :order_item => {:photo_id => photo.id}]

where I’m kind of doing a :build but with more than just the attributes from the builder model being supplied.

UPDATE 11 Apr 2011
I got it working where I can pass an attribute into a get request using polymorphic_url as seen below:

# the helper method
def add_cart_button_for(arr, attr_hash={})
  link_to button(:cart_add), polymorphic_url( [:new] + arr, attr_hash )
end

# the call in index.html.haml
= add_cart_button_for [current_order, :order_item], :photo_id => photo.id

This seems like it should be working. I can see the get request that seems to be working as expected:

Started GET "/orders/1/order_items/new?photo_id=130" for 127.0.0.1 at 2011-04-11 18:11:07 -0400
  Processing by OrderItemsController#new as HTML
  Parameters: {"photo_id"=>"130", "order_id"=>"1"}

and the url is as expected, but the form doesn’t seem to be catching that variable from the url.

The :order_id works. The :photo_id, however, does not appear to be carrying over to the OrderItems#new page. I created a text_field for :photo_id, and it’s empty every time. It’s in the attr_accessible. I don’t see why it’s not working.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T15:15:12+00:00Added an answer on May 21, 2026 at 3:15 pm

    I finally figured this thing out.

    In my OrderItemsController#new action, my build function was either taking no parameters or params[:order_item] as the parameter. Since the parameter passed from the polymorphic_url wasn’t actually related to any object, but was just a raw attribute, I tried simply passing the entire params hash into the build function. Worked like a charm. So I’m using this code for the link:

    = add_cart_button_for [current_order, :order_item], :photo_id => photo.id
    

    and this code in my controller:

    def new
      @order_item = @order.order_items.build(params)
    end
    
    
    
    def create
      @order_item = @order.order_items.build(params[:order_item])
      if @order_item.save
        @order.save
        flash[:notice] = "Successfully created order item."
        redirect_to @order
      else
        render :action => 'new'
      end
    end
    

    Now that the entire params hash is getting passed, the form can find the correct photo when building the new order item.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to loop through a bunch of documents I have to put
I have a JSP page retrieving data and when single or double quotes are
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.