I'm following along here: Text Helper
Specifically I'm using the last example and have in my code:
<%= truncate_html(posts.content) {link_to "Continue", post_path(posts.url_name)}%>
The first part of the truncate works but the link does not appear. Any idea why my link isn't appearing?
I don't know the truncate_html, but you could use this questions answer with a block in the end:
<%= truncate(posts.content, :escape => false) { link_to "Continue", post_path(posts.url_name) } %>
That would create the result you want.
truncate_html does not appear to be a valid method in the Rails base code. Try with truncate
<%= truncate(posts.content) {link_to "Continue", post_path(posts.url_name)}%>
Related
In Rails, I have a "notifications" class, one field of which is "link". The links contained within this class are formatted like: exchange_path(6), where that is the path to the show action in the exchange controller.
I'm now trying to output this link as such:
<%= link_to "View Exchange", notification.link %>
This line is in a loop which begins as such:
<% #notifications.each do |notification| %>
When I click this link, it takes me to localhost:3000/users/exchange_path(6) instead of localhost:3000/exchanges/6 like I would expect. (The loop generating the faulty link is on localhost:3000/users/2)
this could be scary...
<%= link_to "View Exchange", eval(notification.link) %>
should evaluate and use the path helpers. but you need to be 100% sure that nothing bad gets put in the link field..
You could do this:
<%= link_to("View Exchange", "/#{notification.link.gsub('(', '/').gsub(')', '').gsub('_path', 's')}") %>
or set up a method in your model that formats it for you:
def format_link
link.gsub('(', '/').gsub(')', '').gsub('_path', 's')
end
and just call that in your link_to:
link_to("View Exchanges", notification.format_link)
This will only work if all the links are formatted exactly as the example in the question
I have a model called Book and another one called Magazine.
They share the same index view, where pictures of the covers are shown.They are also displayed according to their appearance date, so instances of those two models are mixed in the view...
Each cover has a clickable tite, and leads the user to a description page for this particular book or magazine...
Now in my view,i want to be able to do something like :
<%= link_to document.title, "#{document.class.name.underscore}"_path(document) %>
So in the case of book, i want this line to be replaced by the path from book_path(document) when document is a book,and by the path generated by magazine_path(document) when the document is a magazine.
À la bash script syntax...
How would i realize this.
Thank you very much!
Try:
<%= link_to document.title, polymorphic_path(document) %>
Polymorphic path, when executed with a model, checks the class of passed model, brings it do underscored notation and executes model_name_path. Seems to be exactly what you need.
You can always do this with eval.
<%= link_to "Title", eval("#{document.class.name.underscore}_path(document)") %>
There is also send, which is cleaner, but also metaprogramming:
<%= link_to "Title", send("#{document.class.name.underscore}_path", document) %>
I want to generate the next html link:
http://url.com
To reproduce it using the link_to helper I have to write:
<%= link_to "http://url.com", "http://url.com" %>
What doesn't look DRY at all, I was expecting this to work:
<%= link_to "http://url.com" %>
But the above code generate a link targeting the actual request.url, not the one I'm sending in the param.
Am I missing something?
You're not missing anything --- the normal case is for the URL and the text that shows to the user to be different.
If you'd like, you could create a helper like
def link_to_href(link, args={})
link_to link, link, args
end
then, when you use it,
<%= link_to_href "http://url.com" %>
Will output
http://url.com
If you take a look at the source code of link_to you will see that at line 248 the a tag label is build with name || url.
That's why you have this behaviour and there is noway to do it like you're expecting.
link_to feed_item.votes_for + "Into it!", into_it_micropost_path(feed_item.id)
I basically want the hyperlink to be that variable next to the string "Into it!"
how can i achieve this? thanks
Actually it seems to me your version is supposed to work just fine.
Maybe try to put it in another way:
link_to "#{feed_item.votes_for} Into it!", into_it_micropost_path(feed_item.id)
Don't forget that for the link to appear you will have to put it inside <%= %>
I have this html-Output:
<span>Sign in</span>
How can I use:
click_button I18n.t 'users.sign_in'
I think capybara can not find the button because of the span. What is the best solution to find and test the button?
Cheers
Why don't you use rails tag for submit?
Try <%= submit_tag I18n.t('.users.sign_in') %> and I think it will work.
Well, that is the corresponding .erb-Code:
<%= link_to content_tag(:span,I18n.t("users.sign_in")), "#", style: "margin-left:132px; margin-top:12px;", class: "button-submit-magenta", onclick: "$(this).parents('form:first').submit()" %>
I need the 'span' for compatibility with IE7.
It's not working because you don't have a proper link or button. This will work if you only have one element with the same class on the page:
page.find('.button-submit').click
Try this. Hope it'll work.
page.find(:xpath, "//span[text()='Sign in']").click