I am trying to make a show page that displays an image based on the person called out of the database. My current code looks like this :
<%= image_tag('DAF_520x222.jpg') %>
I want to exchange the 'DAF_520x222' portion for a call of #user_name but I am unsure of how to go about putting this in the image_tag itself.
You can directly interpolate it into the string. It will automatically change it to its value
<%= image_tag("#{#user_name}.jpg") %>
This should work
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'm working on a web application that has a view where data is fetched and parsed from a text file (the textfile is only available at the backend, not to the user). I've written a function that takes in the text file and converts it to an array of strings, it's called txt_to_arr. Then I have another function line_fetcher which just calls txt_to_arr and outputs a random string from the array.
In my view, I call the controller's function as so: <% line_fetcher %>.
I've put both txt_to_arr and line_fetcher into the view controller's helper rb file, and when I run rails s, the random string is not rendered at all. I've also tried <% puts line_fetcher %>
I've checked in Bash that the function does output random strings from the text file, so the function does work correctly. Also, the text file being parsed is in the public folder. Does anyone have an idea why this might be?
Thanks a lot!
Try placing the code in the controller and assigning the output to a variable using
a=`line_fetcher` (note the backtics) as detailed at
http://rubyquicktips.com/post/5862861056/execute-shell-commands
and then <%= a %> in your view.
and place the file in the root of your rails app
Simple erb like <%= line_fetcher %> would work good for simple variables.
But if you want output of any model/database instance then do:
<%= ModelName.first.inspect %>
Note the inspect word.
And in case of using HAML do:
=ModelName.first.inspect
In ERB: The <% %> signify that there is Ruby code here to be interpreted. The <%= %> says interpreted and output the ruby code, ie display/print the result.
So it seems you need to use the extra = sign if you want to output in a standard ERB file.
<%= line_fetcher %>
Use <%= %> to output something in your view, so:
<%= line_fetcher %>
I've been stuck on this problem for days. First off, I now know this code is horribly wrong. I've been trying to fix it, but it's way more important in the short term that this link is created. In my view (I'm so sorry), I call the create method like this, if a certain condition is met:
index.html.erb (controller: subjects_controller)
<%= Baseline.create(subject_id: sub.subject_id) %>
I do this several times on the page, from several controllers (i.e., FollowUp3Week.create(subject_id: sub.subject_id) works). All of the other controllers work. I've checked, and double checked, every controller action and compared them to each other, and they appear the same.
So instead of creating the record, it leaves something like this instead:
#<Baseline:0x007f944c4f7f80>
I'm at a bit of a trouble shooting loss. Once again, I know how wrong it is to have these database actions in the view. But I didn't know that when I made the page, and I really need this to function before I can take the time to learn how to rearrange everything through the MVC.
Any advice would be greatly appreciated. Let me know what other code you might want to look at.
EDIT 1.
link Creation:
<% if Baseline.where(subject_id: sub.subject_id).first != nil %>
<%= link_to "edit", baseline_path(Baseline.where(subject_id: sub.subject_id).first) %>
<% else %>
<%= Baseline.create(subject_id: sub.subject_id) %>
<% end %>
First of all, making DB calls in views is a big NO! NO!
Secondly, to answer why you see the output as
#<Baseline:0x007f944c4f7f80>
for
<%= Baseline.create(subject_id: sub.subject_id) %>
You are trying to render an instance of Baseline model. Its just how the instance would be displayed. If you want to display a particular attribute's value in view then just do
<%= Baseline.create(subject_id: sub.subject_id).subject_id %>
Also, this code will not create a link. To create a link you would have to call link_to helper in your view.
What you need to do is, move the Baseline.create call in the controller. Set an instance variable in the action which renders this particular view as below:
def action_name
#baseline = Baseline.create(subject_id: sub.subject_id)
end
After this in you view you can easily access all the attributes of #baseline instance.
For example:
To access subject_id
<%= #baseline.subject_id %>
To create a link for show page of #baseline, provided you have a RESTful route to show action for baselines
<%= link_to "Some Link", #baseline %>
I am trying to display some info from an API.
I have this code:
<% recco = Model.find_by_id(activity.trackable.id %>
Now, rendering the following line will output the ID I want:
<%= recco.item_id %>
However, when I want to link to this item and I need the name from the API, some of the code fails and some not. This is the line I have:
<%= link_to #client.author(recco.item_id).name, book_path(recco.item_id) %>
The book_path works and displays a link to the correct URL with the ID. The client that tries to get the name from the API returns that it is not found.
But, if I try remove the the recco.item_id and just hardcode it to:
<%= link_to #client.author("7").name, book_path(recco.item_id) %>
It works and displays the author name. Is there a special reason for the API request to not understand that the recco.item_id is a number? The book_path displays the link just fine.
It looks like the author method expects a string instead of an integer. All you have to do is add to_s to the item_id for it to work:
<%= link_to #client.author(recco.item_id.to_s).name, book_path(recco.item_id) %>
The reason why the API does not understand the integer you passed to it is likely because they did not logic have in that method to convert that passed argument into a string. Ideally, a public would try and convert whatever args are passed to it into the type of value it expects. The to_s we did on your method would ideally be in the API, but since you likely don't have control over the API code, you'll have to do the workaround yourself which doesn't take much effort anyway. :)
I'm trying to accomplish this without any plugins.
Basically we just use the standard copy and paste method for embedding Youtube onto our website... the problem, then, is that if we try to share a post of ours with video on facebook... the image thumbnail isn't there.
The thumbnail is always saved in this format:
http://i1.ytimg.com/vi/7Uz1hfza55k/default.jpg
...with the video id coming just before "default.jpg"
I already have three "photo" slots in the database for each post.
So I'd like to do something like this:
<%= image_tag("<%= daily.photo .html_safe %>") %>
I'm just not sure of the proper syntax so that it gets the photo URL for that particular post.
What I want it to return in the end is something like this:
<%= image_tag("http://i1.ytimg.com/vi/7Uz1hfza55k/default.jpg") %>
Getting the URL, of course, from the "photo" section of each post's database entry.
For extra credit maybe you could explain a way that I could arrange it so that all the person writing the articles would have to do is enter the video code and it would be automatically inserted in:
<%= image_tag("http://i1.ytimg.com/vi/CODEHERE/default.jpg") %>
Thank you for your time.
Edit:
Just so we're clear this works:
<img src="<%= #daily.photo %>">
But this doesn't work:
<%= image_tag("<%= daily.photo .html_safe %>") %>
They should be the same as far as I know... but they don't work the same. If worse comes to worse I'll just go with img src...
<%= image_tag(#daily.photo) %>
In ERB, <%= stuff %> means: everything inside this is good old plain ruby. There is no need to use the tag twice, as #daily.photo is just an argument for the image_tag method.