I have the following code:
<% slika = Refinery::Page.find('sladoledi') %>
<%= link_to (image_tag slika.key_image.url, slika) %>
The problem is that it's not linking to slika. Any suggestions?
Try this format
<%= link_to(slika) do %>
<%= image_tag(slika.key_image.url)%>
<% end %>
also have a look at documentation there are nice examples how to use link_to() helper
http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to
First start nesting code you write here. People have problem reading code like this :(
If you want add code click code button on editor.
I suppose the problem is you don't end image_tag.
Correct form is:
link_to(image_tag(slika.key_image.url),slika)
Related
I have the following code:
<%= turbo_frame_tag :my_frame do %>
frame
<% end %>
<%= link_to "About", about_path, data: { turbo_frame: :my_frame } %>
When I click the "About" link, the frame's content doesn't get updated. Instead, the whole page navigates to about_path.
I know that it's not a problem with the above code because I tested the same exact code on a fresh app and it worked fine. Something about this app is different that's making this turbo frame link not work.
Any ideas?
this is a late answer, but I've stumbled upon your question after having the same problem. In my case, I was trying to use turbo_frame_tag inside a table, and it wouldn't work because table wouldn't accept any other elements but its own: https://github.com/hotwired/turbo/issues/48
Turns out you have to have the response be wrapped in the same named turbo tag.
In your initial call
<%= turbo_frame_tag :my_frame do %>
frame content
<% end %>
Your response should be wrapped as such:
<%= turbo_frame_tag :my_frame do %>
response generated on server.
<% end %>
It should be data: {"turbo-frame"=> "my_frame"} in the link_to tag. I think it's not understanding my_frame as a symbol or perhaps the turbo_frame key in the hash.
I just solved this similar situation now. The issue was a typo in the -tag of the view I was requesting. This error showed up in console as:
A matching frame for #show_client was missing from the response, transforming into full-page Visit.
Meaning that you should make sure that the resource you're requesting is in order, or else Rails will fallback to full-page replace of what your link is requesting.
try it:
execute command in terminal yarn add #hotwired/turbo-rails
next add line import "#hotwired/turbo-rails"
into app/javascript/application.js
and run rails by bin/dev instead rails s
it works for me.
I assume you are overriding the navigation. According to the documentation, you code should be
<%= turbo_frame_tag :my_frame do %>
frame
<%= link_to "About", about_path %>
<% end %>
There is a website attr on product_lead table which is optional. If it's present then I wanna turn #produc_lead.lead into a link, but if it's not it should be plain text.
If I use the code below and the website is nil then the link points to the page the user is currently on. If I do it with #product_lead.try(:website), it's gonna be the same. But as I mentioned I would like to have plain text over link in this case.
<%= link_to #product_lead.website, target: "_blank" do %>
<%= #product_lead.lead %>
<% end %>
After playing around I fell back to the following solution, but it's terrible. Any better ideas?
<% if #product_lead.website %>
<%= link_to #product_lead.website, target: "_blank" do %>
<%= #product_lead.lead %>
<% end %>
<% else %>
<%= #product_lead.lead %>
<% end %>
Maybe link_to_if if Rails 4
<%= link_to_if(#product_lead.website, #product_lead.lead, #product_lead.website) do %>
#product_lead.lead
<%= end %>
You can create custom view helper for this.
Well, link_to is going to generate a <a> tag, whether you provide a valid URL or not. So if the URL is nil, yes, it's gonna be a link for you own page.
If you want to "hide" this, you could call a partial in which you place you if/else and so on, but it's just to sweep this under the rug :)
Or if you wanna go further, as #Jovica Šuša, a view helper is the most elegant solution.
Here is my code
Here i want to include youtube video id dynamically. Its not taken here Kindly give me a solution
<%= link_to image_tag("http://img.youtube.com/vi/<%= video.provider_uid %>/hqdefault.jpg"), "http://www.facebook.com" %>
You need string interpolation here, not ERB tag:
<%= link_to image_tag("http://img.youtube.com/vi/#{video.provider_uid}/hqdefault.jpg"), "http://www.facebook.com" %>
I would rewrite the code like that:
<%= link_to "http://www.example.com" do %>
<%= image_tag("http://img.youtube.com/vi/#{video.provider_uid}/hqdefault.jpg") %>
<% end %>
I found a solution
<%= link_to image_tag("http://img.youtube.com/vi/#{video.provider_uid}/hqdefault.jpg"), "http://www.example.com" %>
this is the code
I've just started using rails yesterday, so this is a kinda noob question
for example, a user is at www.example.com/name
and I want to make several links to www.example.com/name/:id
So I tried something like this:
<% #items.each do |item| %>
<%= link_to item.name, '/name' :id %>
<% end %>
I know, it was a complete guess on how I should write the code, but the restful code sends to a completely wrong link. How should I write this three lines?
Use the route helper:
<% #items.each do |item| %>
<%= link_to item.name, item_path(item) %>
<% end %>
ps: when you have a simple question like this one, take a look at this guide, you'll often find the answer.
Try
<%= link_to item.name, item_path(item) %>
item_path is a URL helper method which spits out the link to show a name.
URL helpers have the general form:
{action}_{class}_path({object or object_id})
If {action}_ is omitted, then the default action is assumed (normally show).
How do we add comments in erb files, if we do not want them to be generated into the html content?
Use the <%# %> sequence, e.g.
<%# This is a great comment! %>
For Record
<%# This is a great comment! %>
<%#= This is a great comment! %>
For block comments:
<% if false %>
code to be commented out...
<% end %>
I have a Windows setup, and this <%-# %> sequence is the only one that works for me:
Example:
<%-# This is a sample comment! %>
In my text editor, i run command + / (sublime-text shortcut). It will be like this.
<%
=begin%>
Here is the comment
<%
=end%>
It doesn't look simply, but it works.
Since .erb is by definition "embedded ruby", you can embed every ruby code between: <%= and the other: %>, typically all written in one line. In addition, ruby one-line comments start always with #, so the <%=# Comment %> style matches perfectly with both pure-ruby and erb styles for one-line comments.
I doesn't work in the Controllers files, I had to put it between slashes
/ comment here.... /