Jump to content

The ultimate community for Ruby on Rails developers.


Passing product info from one view to another

rails link_to

  • Please log in to reply
2 replies to this topic

#1 DvDj



  • Members
  • 20 posts
  • LocationUnited Kingdom

Posted 03 September 2014 - 07:03 PM

Cannot see the wood from the trees on this one so any help yuo can provide to this newbie issue will be very much appreciated!

My 'Office' view displays the office products found in the product database. More than one product is displayed within this view so i want users to be able to click on an office product which goes to the 'show' view that displays just that products details.

My store controller looks like this: -

class StoreController < ApplicationController 
def index
@products = Product.all
def show
@products = Product.find_by(:id)
if @products.nil?
redirect_to action: :index

The link_to code within the Office view looks like this: -

<p class="showArticle"><%= link_to 'Show Article', store_show_path %></p> 

The code for the product in the show view looks like this: -

<%= @products.title(:id) %> 

The office products are displayed properly within the office view. When the products link_to is clicked on the browser redirects using the action: :index as @products.nil? is found to be true.

How should i be passing this products details into the show view so that the products details are seen?

#2 Ohm



  • Moderators
  • 530 posts
  • LocationCopenhagen

Posted 07 September 2014 - 08:26 AM

You need to give the store_show_path the id of the product - or the product object, that way Rails will figure out what to do itself.

Blog: http://ohm.sh | Twitter: @madsohm | Work: Lokalebasen.dk

#3 DvDj



  • Members
  • 20 posts
  • LocationUnited Kingdom

Posted 13 September 2014 - 08:37 AM   Best Answer

As ever Ohm,


Many thanks.


If it helps anyone else I have now resolved the problem thanks to help from @mandeep on Stackoverflow, details of which can be found at the following url - http://stackoverflow...other/25669735?

Also tagged with one or more of these keywords: rails, link_to

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users