Is it possible to use link_to_remote with dynamic links?

As part of a search results page, I’m trying allow users of a particular
application to view more details of individual products without 1)
having to load a whole new page, 2) loading details through an IFRAME or
3) having to preload all of the product data into the results page.

Ajax seemed to be an appropriate approach, so I created a blank layer
called “product_details” which is hidden by default and setup the
following code:

<% @products.in_groups_of(8, false) do |row_products| %> <% for product in row_products%> <% end %>
<%= link_to_remote( image_tag('/images/sample.jpg'), :url => { :controller => 'store', :action => 'results' }, :update => "product_details", :complete => "new Effect.Appear('animation', { duration: 3.0 })" ) %>
     </td>
     <% end %>
  1. How could I pass an individual product variable through this link -
    if at all - to the controller?

  2. How can I use a variable in the link_to_remote tag? Specifically, the
    image will always be the ID of the product, but I can’t just insert
    product.id into the code.

Thanks in advance!

<%= link_to_remote( image_tag('/images/sample.jpg'),
  :url => { :controller => 'store', :action => 'results' },
        :update => "product_details",
  :complete => "new Effect.Appear('animation', { duration: 3.0 })" )
  1. How could I pass an individual product variable through this link -
    if at all - to the controller?

…if you mean you want to pass a product object to the controller, u
pass the object id to the controller, and find it, ie:

:url => { :controller => ‘store’, :action => ‘results’, :prod_id =>
product.id },

  1. How can I use a variable in the link_to_remote tag? Specifically, the
    image will always be the ID of the product, but I can’t just insert
    product.id into the code.

if you have an image per product, and want to display it use the “#{}”
ruby string#eval thing

 <%= link_to_remote( 

image_tag("/images_path/prod_#{product.id}.jpg"),

(i feel like i’m missing something in your request, but the above is the
simplest thing that comes to my mind)

hth

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs