How to place an image inside of a link?

2019-07-04 04:14发布

I'm trying something really simple, inside a link I want there to be text and an image.

= link_to 'NVidia Graphics', inventory_url, class: 'lato' do
  = image_tag 'list-highlighter.png'

I'd like the output to be something like:

<a href="/inventory">
  NVidia Graphics
  <img src="list-highlighter.png" />
</a>

How can I achieve this using Slim? My current code causes the website to crash.

undefined method `stringify_keys' for "http://foobar.com/inventory":String

2条回答
狗以群分
2楼-- · 2019-07-04 04:30

Soluction

= button_to('Add', line_item_path, method: :post , class: "btn btn-warning btn-lg" , params: { param1: 'value1',  param2: 'value2' })

Rendered

<form class="button_to" method="post" action="/line_items/17">
<input class="btn btn-warning btn-lg" type="submit" value="Add" />
<input type="hidden" name="authenticity_token" value="Qk2sdfgasdfasdfsfa sdfsfsw==" />
<input type="hidden" name="param1" value="value1" />
<input type="hidden" name="param2" value="value1" />
</form>

http://qiita.com/tomomomo1217/items/a5f790c31670587e2d87

查看更多
Ridiculous、
3楼-- · 2019-07-04 04:36
= link_to inventory_url, class: 'lato' do
  | NVidia Graphics
  = image_tag 'list-highlighter.png'

I think that should work.. just not 100% about the slim syntax. link_to shouldn't have any content when wrapping something as a block -- that is, it should be immediately followed by its url. All the content inside will be wrapped by the <a> tag output. For non-slim, this would look like

<%= link_to inventory_url, class: 'lato %>
  NVidia Graphics
  <%= image_tag 'list-highlighter.png' %>
<% end %>
查看更多
登录 后发表回答