问题
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