Jump to content

The ultimate community for Ruby on Rails developers.


Photo

syntax problem on link_to


  • Please log in to reply
4 replies to this topic

#1 DvDj

DvDj

    Signalman

  • Members
  • 20 posts
  • LocationUnited Kingdom

Posted 22 August 2014 - 03:58 PM

Another newbie issue from me I'm afraid!!

 

Due to my lack of knowledge regarding Rails i have come up against another issue today, this time linking to an image.

<%= link_to( image_tag(office.image_url, :class => "lightbox_trigger"), office.image_url) %>

So Rails is cycling through items in a database and inserting the 'image_url' that is the html reference to the image itself and displays it.  This is wrapped in a link_to tag that currently generates the following HTML.

<a href="/store/office"><img alt="1984" class="lightbox_trigger" src="/assets/group_shots/1984.jpg" /></a>

How do i make the anchor href show as <a href="/assets/group_shots/1984.jpg">?



#2 Ohm

Ohm

    Driver

  • Moderators
  • 479 posts
  • LocationCopenhagen

Posted 23 August 2014 - 10:47 AM

You want to use the image_path helper here. (http://apidock.com/r...lper/image_path)

<%= link_to image_tag(office.image_url, class: 'lightbox_trigger'), image_path(office.image_url) %>


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


#3 DvDj

DvDj

    Signalman

  • Members
  • 20 posts
  • LocationUnited Kingdom

Posted 24 August 2014 - 04:31 PM

Ohm,

 

you are always a huge help to me and your time is very much appreciated.

 

The code you showed does indeed return the HTML i thought i needed i.e. '<a href="/assets/group_shots/1984.jpg">', however i have now realised that Rails leaves out the 'images' folder in the url.

 

When the image is clicked on, 'class: lightbox_trigger' is triggered which uses the images 'href' to display the image and i am thinking that because Rails provides the URL '/assets/group_shots/1984.jpg' which leaves out the 'images' part of the address, it can't find the image and is failing.

 

Within the image_path options is there an option that will pass an href that will be understood? 

 

as i have 'products' listed as an application resource and the images are found from here i did try the 'path_to_image' alias but this resulted in the same issue.



#4 Ohm

Ohm

    Driver

  • Moderators
  • 479 posts
  • LocationCopenhagen

Posted 24 August 2014 - 08:43 PM   Best Answer

The correct path to the images (at least in development) are without the 'images' of the URL. 

 

Rails will route the URL correctly to the image.

 

If you take a look at the generated source, it should be something like:

<a href="/assets/group_shots/1984.jpg" class="lightbox_trigger">
  <img src="/assets/group_shots/1984.jpg" />
</a>

Correct? Are you sure the class needs to be on the anchor and not on the image? Are you sure that the Javascript is working correctly? If it's showing the image, and the URLs are identical, then I see no reason for it not to work.


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


#5 DvDj

DvDj

    Signalman

  • Members
  • 20 posts
  • LocationUnited Kingdom

Posted 25 August 2014 - 04:56 PM

Hi Ohm,

 

Thank you again for your help, you are absolutely right there is nothing wrong with the links the problem must be elsewhere.  I now understand the link helpers a little more :)






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users