developer tip

link_to 이미지 태그.

optionbox 2020. 9. 7. 08:03
반응형

link_to 이미지 태그. 태그에 클래스를 추가하는 방법


다음과 같이 link_to img 태그를 사용하고 있습니다.

<%= link_to image_tag("Search.png", :border=>0, :class => 'dock-item'), 
:action => 'search', :controller => 'pages'%><span>Search</span></a>

다음 HTML 결과

<a href="/pages/search"><img alt="Search" border="0" class="dock-item" 
src="/images/Search.png?1264132800" /></a><span>Search</span></a> 

class = "dock-item" <a>이 img 태그 대신 태그 로 이동하기를 원합니다 .

어떻게 변경할 수 있습니까?

최신 정보:

<%= link_to image_tag("Search.png", :border=>0), :action => 'search', 
:controller => 'pages', :class => 'dock-item' %>

결과

<a href="/pages/search?class=dock-item"><img alt="Search" border="0" 
src="/images/Search.png?1264132800" /></a> 

안녕하세요 당신은 이것을 시도 할 수 있습니다

link_to image_tag("Search.png", border: 0), {action: 'search', controller: 'pages'}, {class: 'dock-item'}

또는

link_to image_tag("Search.png", border: 0), {action: 'search', controller: 'pages'}, class: 'dock-item'

중괄호를 놓치면 레일이 단일 해시 매개 변수를 형성한다고 가정하기 때문에 중괄호의 위치는 매우 중요합니다 ( 여기에서 자세히 알아 보세요 ).

그리고 link_to 에 대한 api 에 따르면 :

link_to(name, options = {}, html_options = nil)
  1. 첫 번째 매개 변수는 표시 할 문자열입니다 (또는 image_tag 일 수도 있음).
  2. 두 번째는 링크의 URL에 대한 매개 변수입니다.
  3. 마지막 항목은 html 태그를 선언하기위한 선택적 매개 변수입니다 (예 : class, onchange 등).

도움이 되길 바랍니다! =)


추가하면 link_to메소드에 블록을 전달할 수 있습니다 .

<%= link_to href: 'http://www.example.com/' do %>
    <%= image_tag 'happyface.png', width: 136, height: 67, alt: 'a face that is unnervingly happy'%>
<% end %>

결과 :

<a href="/?href=http%3A%2F%2Fhttp://www.example.com/k%2F">
    <img alt="a face that is unnervingly happy" height="67" src="/assets/happyface.png" width="136">
</a>

디자이너가 멋진 css3 롤오버 효과가있는 복잡한 링크를 제공했을 때 이것은 생명의 은인이었습니다.


최고는 다음과 같습니다.

link_to image_tag("Search.png", :border => 0, :alt => '', :title => ''), pages_search_path, :class => 'dock-item'

이것은 내 해결책입니다.

<%= link_to root_path do %>
   <%= image_tag "image.jpg", class: "some class here" %>
<% end %>

쉬운:

<%= link_to image_tag("Search.png", :border=>0), :action => 'search', :controller => 'pages', :class => 'dock-item' %>

link_to 의 첫 번째 매개 변수는 링크 할 텍스트 / html입니다 ( a 태그 내부 ). 다음 매개 변수 세트는 url 속성과 링크 속성 자체입니다.


업데이트 된 질문에 응답하려면 http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html ...

추가 리터럴 해시가 필요하므로 이전 인수 스타일을 사용할 때는주의해야합니다.

  link_to "Articles", { :controller => "articles" }, :id => "news", :class => "article"
  # => <a href="/articles" class="article" id="news">Articles</a>

해시를 해제하면 잘못된 링크가 제공됩니다.

  link_to "WRONG!", :controller => "articles", :id => "news", :class => "article"
  # => <a href="/articles/index/news?class=article">WRONG!</a>

:action =>, :controller =>내가 많이 본 모든 부분이 저에게 효과가 없었습니다.

Spent hours digging and this method definitely worked for me in a loop.

<%=link_to( image_tag(participant.user.profile_pic.url(:small)), user_path(participant.user), :class=>"work") %>

Ruby on Rails using link_to with image_tag

Also, I'm using Rails 4.


I tried this too, and works very well:

      <%= link_to home_index_path do %>
      <div class='logo-container'>
        <div class='logo'>
          <%= image_tag('bar.ico') %>
        </div>
        <div class='brand' style='font-size: large;'>
          .BAR
        </div>
      </div>
      <% end %>

Hey guys this is a good way of link w/ image and has lot of props in case you want to css attribute for example replace "alt" or "title" etc.....also including a logical restriction (?)

<%= link_to image_tag("#{request.ssl? ? @image_domain_secure : @image_domain}/images/linkImage.png", {:alt=>"Alt title", :title=>"Link title"}) , "http://www.site.com"%>

Hope this helps!


<%= link_to root_path do %><%= image_tag("Search.png",:alt=>'Vivek',:title=>'Vivek',:class=>'dock-item')%><%= content_tag(:span, "Search").html_safe%><% end %>

You can also try this

<li><%= link_to "", application_welcome_path, class: "navbar-brand metas-logo"    %></li>

Where "metas-logo" is a css class with a background image

참고URL : https://stackoverflow.com/questions/2115214/link-to-image-tag-how-to-add-class-to-a-tag

반응형