Rails 3.1, can't make link_to image_path?

核能气质少年 提交于 2019-12-24 19:15:13

问题


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?


回答1:


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

Put some parentheses




回答2:


You need some ()

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

Also, you need to use image_tag




回答3:


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

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




回答4:


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

<%= link_to(image_tag("town.png", :alt => 'Town Image'), index_url) %>


来源:https://stackoverflow.com/questions/8333928/rails-3-1-cant-make-link-to-image-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!