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!
Related
Rails 7 / Ruby 3
I'm currently working on a site that requires code examples to be displayed on a page - I can get these to display utilising the extra % character trick, however, for some of the examples I need to have a variable within them that is resolved (e.g. like the users' own API key etc...).
Consider I have #variable = "Resolved Variable"
<%%= link_to #variable, variable_path %>
Outputs on the page explicitly as
<%= link_to #variable, variable_path %>
But I really need the #variable to resolve and show on the page as:
<%= link_to "Resolved Variable", variable_path %>
I've tried all kinds of escaping the variable, but it seems that <%%= ensures that nothing following it can be resolved.
Any ideas?
Any text you haven't html_encodeed will be displayed as plain text.
My suggestion to you is to create a interpolated string that you could use to generate your intended result. For example:
output_text = "<%= link_to '#{#variable}', variable_path %>"
And, not sure if this is what you're looking for, but you can get a good UI by adding some Javascript library to format you code in the language in intend (in this case Ruby, it seems).
In case that's interesting to you, check the Prism lib, or check how to add it to your project here
I hope this helps.
With kind regards,
Rogerio
<%% in ERB will simply output <%, no more, no less. In particular, it won't attempt to parse the code after <%% as Ruby. However, this doesn't mean that you can't have another <%= ... %> after <%%:
require 'erb'
template = <<-EOD
<%%= link_to <%= #variable.inspect %>, variable_path %>
EOD
#variable = "Resolved Variable"
puts ERB.new(template).result
The inspect method will add quotes around your string and also escape certain characters as needed.
Output:
<%= link_to "Resolved Variable", variable_path %>
I am currently building an online course and I was having trouble accessing a lesson that is associated with a certain course.
A developer friend of mine solved the problem for me but I'm not really sure why this code works and if there are different ways, more of a Rails conventional way to write this code.
<% #courses.each do |course| %>
<tr>
<td><%= link_to course.title, "courses/#{course.id}" %></td>
</tr>
<% end %>
I am not sure what this part "courses/#{course.id}" is doing. Is there a way to write this using a more conventional seeming names route helper?
It should be the same as course_path(course)
This call just figure out the path for you. The expression in your code simply build this path putting together "courses/" and the id of the course (but using interpolation, not concatenation).
As Ursus answer explains, courses/#{course.id} creates URL directing to specific course path by using string interpolation. For example, if #courses variable is an array with Course objects with ids: [1, 2, 3], then you will receive links directing to "course/1", "course/2", course/3".
To replace that interpolation, you can simply write
<%= link_to course.title, course %>
It will create the same output as "courses/#{course.id}"
To learn more about string intepolation, you can start here: http://ruby-for-beginners.rubymonstas.org/bonus/string_interpolation.html
For some reason your friend did this:
<td><%= link_to course.title, "courses/#{course.id}" %></td>
...instead of this:
<td><%= link_to course.title, course %></td>
...and I have no idea why. The second example is how you use links in Rails. The first example doesn't safeguard you against possible future URL changes.
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) %>
In my view, I'm using this to display the user
Made a comment on <%= link_to activity.trackable.micropost.user, activity.trackable.micropost.user %>
When I do this, it works, but the link shows up as something like #<User:0x5424a68>
I tried using activity.trackable.micropost.user.username, activity.trackable.micropost.user.name, and other variations but they didn't work.
What do I need to add after .user?
The activity.trackable is from the PublicActivity gem.
Open rails console and type:
User.instance_methods.grep(/name/)
It will give you a list of methods on User that contain the string 'name'. Chances are, that you will find the method you are looking for in the list (if there is any).
Try
<%= activity.trackable.micropost.user.inspect %>
or
<%= activity.trackable.micropost.user.to_s %>
This should give you a decent idea of what you need to add..
i'm creating an application where i need to send a private message to a user and ask him/her for a confirmation on whether they would like to join or not. I have created a private message module and whenever i want to send a message, i do something like :
def sendMessage(attributes)
subject = 'whatever'
body = 'whatever'
current_user.sendMessage(current_user, subject, body)
end
Then, i get this message and print it out to the needed places, using <%=h %> for escaping stuff. My problem now is, what happens if i want to include 's or even more importantly <%= link_to %> inside that ?
How can i insert such things to be printed out and also be careful about escaping the user provided attributes ? I was thinking of creating sort of like a partial to do this for me, but i would certainly like to hear what you think about it.
Thank you :)
First off, you probably should name your method send_message since that tends to be the convention in Ruby and Rails.
For the question, why not do this:
<p><%=h user_submitted_info %> and check out this <%= link_to "Awesome Link", "/" %></p>
This will escape the user submitted content but leave the link_to alone. Why does the user submitted content and the link need to be in the same ERB tags?