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 %>
Related
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.
I have a model, Notification, that has two fields: text and link. In my view for notifications, I have the following:
<% #notifications.each do |notification| %>
<li>
<%= notification.text %>
<%= link_to "View", notification.link %>
</li>
<% end %>
Examples of links include:
"foos/4/bars"
"about"
"foos"
However, when I attempt to follow the link, if I am in the "baz" controller, the result is an attempt at "baz/foos/4/bars", or "baz/about", rather than just "foos/4/bars" or "about".
Is there a better way to do this, or a way to disable the appending of the link to the current controller?
You trying to get a relative path to your current controller.
Try doing this ->
<%= link_to "View", "/" + notification.link %>
Thanks to #Kumar Abinash. The path was relative without a prepending "/". Simply changed links in the database to "/..."
Pretty new to rails. I'm doing a project where a user submits a url and a title. The title is supposed to link to the url provided by the user. The url is stored as a param for link.
Here's the code from the index view:
<% #links.each do |link| %>
<%= link_to link.title, link.url %>
<%= link_to "comments", link %>
<% end %>
This works, for the most part.
The problem occurs if the submitted url doesn't begin with http://. As it is, it's pointing to http://localhost:3000/google.com and I get an error No route matches [GET] "/google.com"
How could I get around this? I tried changing it to:
<%= link_to link.title, "http://#{link.url}" %>
Which makes google.com work, but then http://google.com turns into http://http//google.com somehow.
I'm sure the fix will be a face palm moment!
Prepend url with protocol if it's absent:
module ApplicationHelper
def url_with_protocol(url)
/^http/i.match(url) ? url : "http://#{url}"
end
end
<%= link_to link.title, url_with_protocol(link.url) %>
answer derived from this SO question/answer
In your input field, you can do something like <input type="text" name="url" value="http://"> so that your url will always started with http://. User can also manually changed it to https if needed.
Also I may add a full_url method to the model that adds it if it's missing.
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)
I have this tag: <%= link_to 'Show', user_listing_url(listing.user, listing) %> but instead of simply having it say 'Show' I actually want to place HTML inside of the <a> tag. Is this possible?
Example:
<div><div><img /></div></div>
yes you can pass a block to link_to
try something like this:
<%= link_to(user_listing_url(listing.user, listing)) do %>
<div><div><img/></div></div>
<% end %>
I totally recommend marflar's answer above.
However I would add one comment which is that if you are using html elements within a link_to block this may apply rails default link styling which may not be desirable.
One alternative is to use a button_to link but don't forget the default method for this is POST so specify the options as GET:
button_to(user_listing_url(listing.user, listing), method: :get) do %>
<div></div>
<% end %>