image_tag doesn't behave - ruby-on-rails-3

OK, we are ALL at a loss here. Trying to display an image with image_tag. The path is stored in a sqlite3 varchar(64) field, with quotes around it. The field is called imagepath.
On the show page...
This: <%=#invitem.imagepath%>
gives me THIS:
"/images/5585L.jpg""
This:
<%=image_tag(#invitem.imagepath) %>
gives me THIS:
5585l (note the lower case L)
This:
<%=image_tag("/images/6287L.jpg")%>
Renders the image I want.
Anybody got a clue?
Stuart

Try this:
<%= image_tag("#{#invitem.imagepath}") %>

Related

Dynamic buttons in Rails 3

I have a link_to image_tag that when clicked should pop up a modal to show a help screen. The button code is:
<%= link_to image_tag('help_sm.png'), '#', :id =>"btnShowHelp_"{current_step} %>
where the {current_step} is the name of the step the user is on that corresponds to the help screen they are accessing.
I am getting a SyntaxError outputting the current_step.
Can someone help with the correct syntax?
Interpolation in strings in ruby looks like this: "#{var}", so you need to do this:
<%= link_to image_tag('help_sm.png'), '#', :id =>"btnShowHelp_#{current_step}" %>
Note that you have to use double quotes(") for string interpolation, if you use single quotes('), it won't interpolate.

Rails 3 - Object.try syntax

How would I avoid nil checks using Object.try for the following?
<%= image_tag(PeriodState.where("sla_id = ?", sla.id).last.state.image_file) %>
I've tried .try many different ways, but still receive errors, so my syntax is off. Thanks in advance!
try isn't really appropriate for this: whatever the outcome image_tag will always be called - so you might end up calling it with nil. You need to check whether the image exists first then create an image tag only in this case. So I would get the PeriodState in your controller and have a simple if in your view:
# in controller
#period_state = PeriodState.where("sla_id = ?", sla.id).last
# in view
<%= image_tag(#period_state.state.image_file) if #period_state %>
Of course this won't work if either state or image_file could also be nil.

Rails 3 - how to work with data got from database

I am loading from database the only row. The data are stored in variable (e.g.) #data.
In view, if I want to display the value got from database, I have to do following:
<% #data.each do |d| %>
<%=d.name %>
<%end%>
And I would like to ask you - exist any better way? I think it's a bit silly for the only row to use loop... I tried something like
<%= #data.name %>
OR
<%= #data.each.name %>
But in both cases I got the error message about bad syntax...
So to my question - is possible to get display data a bit more elegantly?
EDIT: my query: #data = Car.includes(:tyres).where("param1 = ?", params[:param1])
If you've loaded more than one model (row), then a loop is the natural construct for displaying each value. If you're really set on a one-liner, you could use some of Ruby's list comprehensions:
<%= #data.map(&:name).join(" ") -%>
I think that you are loading .all instead of .first.
In your controller,
#data = Data.where(:some => 'condition').first
or
#data = Data.find(params[:id])

Ruby/Rails - Is there an easy way to make hard-coded HTML symbols html_safe?

In my view I want to display some right double angle quotes in my link.
Before Rails 3, this worked:
<%= link_to "» #{#category.name}", some_path %>
Now what should I do if I want to specify the » as html_safe but not the rest of the link's text?
In other words I do not want to do this:
<%= link_to "» #{#category.name}".html_safe, some_path %>
I do not want the #category.name treated as html_safe.
This produces the desired result:
<%= link_to "»".html_safe + " #{#category.name}", some_path %>
However, if I do this:
<%= link_to "#{#category.name}" + "»".html_safe, some_path %>
The output of the angle quotes is not treated as safe. I see » on my page and not ».
Why?
I tried extracting "»".html_safe to a helper method with the same results.
Is there a way to easily designate hard coded text/symbols as HMTL safe in Rails 3?
Thanks.
In this situation I often explicitly escape the unsafe part:
"» #{h #category.name}".html_safe
you need to make sure that the whole string is html_safe...
I'd recommend to try this:
"» #{h #cagegory.name}".html_safe

Loop in view is showing more result as expected by calling super(value.to_s)

I am running Rails 3 with the following code in the view
View:
<%= #found_docs.each do |doc| %>
<%= doc.id | doc.content %>
<% end %>
As a result I get two objects, as expected - but in addition a third result is displayed. It is created by calling super(value.to_s) on the result set. Doing #founds_doc.count returns 2 as expected.
Why is the third object displayed when running the block, when #found_docs has only two objects ?
I found the solution - and I did a silly mistake !
The view code is as follow:
<%= #found_docs.each do |doc| %>
I put the "=" sign in front of the loop, hence the result of the loop is printed with value.to_s in the view.
Correct code is:
<% #found_docs.each do |doc|%>
Must have been blind :-)