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 <%= %>
Related
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)}%>
I am having a an issue.
<%= link_to "<button>Add</button>".html_safe, new_admin_course_path, :id=>"open-contacts-dialog-btn", :class=>"inbox-sf-add-btn tip" %>
What if I want to add a ruby variable and some normal text the button? e.g. $25,- (where the $ and ,- are fixed and the 25 variable...I am quite new to this...sorry if this is too easy, but struggling. I tried a lot of options and googled for long time.
The correct helper you want for this is button_to.
To use a variable in the button text is defined as string iterpolation. In your example it could be something like:
<%= button_to "$#{cost}", new_admin_course_path %>
Check out the button_to api reference for more options.
you could do like
amount = 25 #amount is the ruby variable
<%= link_to "<button>Add $#{amount}</button>".html_safe, new_admin_course_path, :id=>"open-contacts-dialog-btn", :class=>"inbox-sf-add-btn tip" %>
and I personally dont add <button></button> to the link and I rather use CSS to get the look and feel
Put your link inside a <button></button> tag. It'll be more readable.
And use variables interpolation in double-quoted strings to insert variable's value into string (the #{#variable_name} part)
<button>
<%= link_to "$#{variable_name}", new_admin_course_path, :id=>"open-contacts-dialog-btn", :class=>"inbox-sf-add-btn tip" %>
</button>
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.
There is probably a really simple answer to this but, as I'm a Rails newbie, I'm having great difficulty identifying the appropriate syntax.
Basically, I want to display a string with a link on the end, in which "Jimmy" here represents both the individual record and the link to that record:
"This video was posted by Jimmy"
I'd like to create a local variable to store the string, so my initial thought was to create the variable as follows:
my_string = "This video was posted by " + (link_to user.name, user)
However, this doesn't appear to work. Instead, it simply displays the generated HTML in the browser, i.e.:
This video was posted by Jimmy
This isn't what I want - I obviously want it to display:
This video was posted by Jimmy
in which Jimmy is the link.
Any ideas? I've tried adding .html_safe to the end of the string, but that doesn't work.
Thanks!
A much easier way to do this would be:
<td>Video created by <%= link_to user.name, user %></td>
No need to use string concatenation or use <%= "Video created by" %>, there's no need to run that through the Ruby parser, just use the plain text version :)
Check out raw
<%= raw my_string %>
Though, assuming this is in your view, I don't know why you'd be storing this to some my_string variable.
<span>This video was posted by <%= link_to user.name, user %></span>
Thanks for your help! I managed to fix the issue without needing to declare a variable (I was trying to be too clever).
The final (elided) code, in case anyone is interested, is as follows (in a table cell):
<td><%= "Video created by " %><%= link_to user.name, user %></td>
As always, it turns out the code is much easier to apply once you know how :-)
Thanks again!
At the moment
<%= link_to comment.file, comment.file_url %>
displays
/uploads/comment/file/6/IP___Addresses
Is there such thing as something like comment.file.filename ?
Is there a way to get the filename and display a link to that, so it would just say IPAddresses.txt and links to "/uploads/comment/file/6/IPAddresses" ?
Edit:
Figured it out
<%= link_to File.basename(comment.file.url), comment.file_url %>
You could have used the *_identifier method, in your case:
comment.file_identifier