Rails 3.1, can't make link_to image_path?

2019-08-24 01:21发布

I want to make a fairly straightforward image link that uses an image from my assets. Getting weird errors. First I tried:

<%= link_to assets_path "town.png", 'index' %>

and got the error

Started GET "/" for 127.0.0.1 at Wed Nov 30 17:27:10 -0500 2011
Processing by PagesController#intro as HTML
Rendered pages/intro.html.erb within layouts/application (114.9ms)
Completed 500 Internal Server Error in 124ms

ActionView::Template::Error (undefined method `assets_path' for #<#<Class:0x10fdc4898>:0x10fdaad58>):
1: <body onload="init();">
2:  <div id = "wrapper2">
3:  <div class="intro_txt">
4:      <%= link_to assets_path "town.png", 'index' %>
5:      <br><br>
6:  </div>
7:  </div>
 app/views/pages/intro.html.erb:4:in     `_app_views_pages_intro_html_erb__1651075534_2280428740'

then I tried the old

<%= link_to image_tag "town.png", 'index' %>

and got this bizarre error

ActionView::Template::Error (undefined method `symbolize_keys!' for "index":String):
1: <body onload="init();">
2:  <div id = "wrapper2">
3:  <div class="intro_txt">
4:      <%= link_to image_tag "townProjectText.png", 'index' %>
5:      <br><br>
6:  </div>
7:  </div>
app/views/pages/intro.html.erb:4:in `_app_views_pages_intro_html_erb__1651075534_2279838600'

What to do?

4条回答
不美不萌又怎样
2楼-- · 2019-08-24 01:56

Yeah some brackets would perhaps help. Try something like this:

<%= link_to(image_tag("town.png", :alt => 'Town Image'), index_url) %>
查看更多
仙女界的扛把子
3楼-- · 2019-08-24 02:02
<%= link_to image_tag('town.png'), 'index' %>

Put some parentheses

查看更多
beautiful°
4楼-- · 2019-08-24 02:04

You need some ()

<%= link_to image_tag("town.png"), pages_path %>

Also, you need to use image_tag

查看更多
做个烂人
5楼-- · 2019-08-24 02:11
<%= link_to image_tag("town.png"), image_path %> 

if you want it to go to another page with the image by itself

查看更多
登录 后发表回答