xps15z
xps15z

Reputation: 1829

Adding link_to method to image_tag

On my index page I am displaying all registered users. When I click on their profile image it takes me to their page, however when I right click on the image to open in new tab it takes me to their photo url address only.

How can I add link_to so that when I right click on the image it opens the path to the user profile instead of the image url.

The related line should be:

  = image_tag user.avatar.try(:image_url)

_user.html.slim:

- if user.avatar.present?
  .common_box.box1 data-user-id="#{user.id}"
    .img_box
      = image_tag user.avatar.try(:image_url)
      -if @user.present?  
        ul.btn_link.hide
          li
            a.message_btn  href="#" data-user="#{user.id}"  data-mfp-src='#message_me'   Message
          li
            = link_to "Save", follow_popup_user_path(user), class: 'save_btn', :'data-mfp-src'=>'#follow_div', remote: true
          li
            a.report_btn href="#" data-mfp-src='#report_me'  Report
    .img_detail
      small years
      .circle
        span.age_box class="#{user.gender == 'Male' ? '': 'pink'}" = user.age
      h3 class="#{user.gender == 'Male' ? '' : 'pink'}" = user.username
      h4 
        = user.address
      div class= "#{user.gender == 'Male' ? 'green_corner': 'pink_corner'}"
        =image_tag "#{user.gender == 'Male' ? 'side_curv.png': 'curv_2.png'}" 

Upvotes: 0

Views: 298

Answers (1)

tehfailsafe
tehfailsafe

Reputation: 3451

Since you have a user defined as user already (guessing this is from a _user partial?) you should be able to something as simple as putting the image tag inside a link_to block.

- if user.avatar.present?
  .common_box.box1 data-user-id="#{user.id}"
    .img_box
      = link_to user do
        = image_tag user.avatar.try(:image_url)
      -if @user.present?
      // etc

Make sure the indentation is correct or slim will try to put everything indented past the link_to block in a link and the error that sometimes displays is tough to hunt down.

Upvotes: 2

Related Questions