Jump to content

The ultimate community for Ruby on Rails developers.


Changing an area of content upon click

rails partial view

  • Please log in to reply
2 replies to this topic

#1 DvDj



  • Members
  • 20 posts
  • LocationUnited Kingdom

Posted 20 August 2014 - 08:05 AM

I have a view working on the following code:-

<% @products.each do |office| %>
				<div class="item">
					<%= image_tag(office.image_url) %>
					<p><strong><%= office.item_code%></strong>
					</br><em><%= truncate(office.title, length: 18) %></em></p>					
					<p class="showArticle"><%= link_to 'Show Article', office %></p>
					<p class="addTo"><a href="#">Quote this item</a></p>
			<% end %>

The above is in the 'Store' view and calls on the records held in the 'Product' table and renders them each in turn here.  I have a link_to helper, 'Show Article' that upon click is supposed to replace the <div class="item"> content with the content of that one item.  Instead the browser is redirected to the 'Show' page for that item within the 'Products' section of the app.


Can anyone offer me any pointers in how i render a new view within this location?  I was hoping for a technique that will carry the items information over to a new view that would allow me to choose the information from that item i want presented to the user and styled in the way i want it styled.


Any help given is much appreciated.

#2 pgaspara



  • Members
  • 3 posts

Posted 12 September 2014 - 06:41 PM

I am not sure I fully understood what you are trying to do, but in order to replace the ".item" div you would have to do it via ajax. link_to would have another attribute 'remote: :true' that would make an ajax call to the show action, you would then respond with a show.js file that will be in charge of replacing the content of the ".item" div. That's just one way of using ajax in Rails.


Alternatively, if what you are going to show is not that much data, you could "preload" it by having it in hidden div and just displaying it with regular javascript when you click on the link.


As a side note, for someone reading your code, it is confusing to see that you are looping through @products, yet each item is called office, it would be clearer at least for me to do @products.each do |product| 

#3 DvDj



  • Members
  • 20 posts
  • LocationUnited Kingdom

Posted 13 September 2014 - 08:42 AM   Best Answer

Hi pgaspara,


As a newbie i am finding quite difficult to explain issues properly.  Thank you for you reply, since this original post i have resolved my issue with the help of @mandeep on Stackoverflow;details of which can be found at http://stackoverflow...other/25669735?

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

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users