How to ask if something is .present? in a view in Roda - roda

I am converting a Rails app to Roda. Here's a section of a partial.
# _banner.erb
<% if banner.present? %>
...
<% end %>
This returns the error: NoMethodError: undefined method 'present?' for []:Array.
How can I get Roda to support something simple like checking if a variable is present?

All present? does is negate blank? which in turns looks like this. You could add this to your helper/service classes depending on your setup.
def present?
!blank?
end
def blank?
respond_to?(:empty?) ? !!empty? : !self
end

Related

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 STI Mystery - Why does type change from Class to String in view?

This is long so I hope you'll bear with me...
I have a model called Update with two subclasses, MrUpdate and TriggeredUpdate. Using single-table inheritance, added type field as a string to Update.
In my view I'm checking which type it is to decide what to display. I assumed since type is a string, I should do
<% if #update.type == 'MrUpdate' %>
This failed, i.e., it evaluated to false when the update was an MrUpdate. I noticed that at this point, #update.type.type is Class. OK, whatever, thought I, so I changed it to:
<% if #update.type == MrUpdate %>
and it worked, i.e., the comparison evaluated to true when the update was an MrUdpate. Then I did it again lower down in my view and it failed again (i.e., it evaluated to false when the update was an MrUpdate.)
Turns out the culprit is a couple of <%= link_to ... %> calls I use and make into buttons with jQuery. If I put this code in my view:
<br>
<%= #update.type.type %><br>
<%= #update.type %><br>
<%= link_to 'New Note', new_note_path(:update_id => #update.id), :class => "ui-button" %>
<br>
<%= #update.type.type %><br>
<%= #update.type %><br>
What I see is:
Class
MrUpdate
(the New Note button)
String
MrUpdate
It's changing from a class to a string! So what the heck am I doing wrong or missing here? Why should a link_to do that? First I'm not clear why it's not a string in the first place, but then really confused as to why it would change...?!? Any help or explanation would be helpful. I can just code it one way at the top and another way at the bottom, but that way madness lies. I need to understand why this is happening.
I figured out what the issue is here. Thanks to fl00r for pointing the way.
Yes, type is a reserved in Ruby 1.8.7 which tells you the class of the object you call it from. But it's also true that it is the name of the field used in Rails to indicate single-table inheriance and to store the name of the class of each instance of the subclass.
So I naively tried to access the value of the type field using #update.type. But what this was doing at the top of the view was calling the type method of the Object class in Ruby. For whatever reason, after the link_to calls, it was then access the value of the type field of the updates table.
While trying to figure this out I called #update.type in the Rails console and saw this message: "warning: Object#type is deprecated; use Object#class". Finally it registered what I was doing. When I changed my calls to:
<% if #update.class == MrUpdate %>
everything works as expected. I never saw a call to determine the type in any of the pages I found via Google about STI. This despite the fact that they all recommended using only one controller, wherein sometimes you must need to determine the class of the instance you have.
So, dumb mistake--pilot error. But maybe this will help someone else who gets tripped up on this.

RoR, Passing a variable into view gives a null object

I am setting a variable in my controller but for some reason it is not getting set. I tried it two ways.
def update
# #available_cars = Car_info.where("user_id = ?", session[:user_id])
#available_cars = Car_info.find_by_user_id(session[:user_id])
end
In my view I do this.
<% #available_cars.each do |car| %>
<%= car.name %>
<% end %>
What I intend to do is populate the #available_cars into a drop down list but I can't even get them to print. I know the session[:user_id] is set because I print it and access it elsewhere.
I get this error...
Expected D:/RailsProjects/mileage/app/models/car_info.rb to define Car_info
in my controller
app/controllers/active_car_controller.rb:6:in `update'
Any help would be appreciated. New to RoR.
I see your controller method is named 'udpate' instead of 'update' - could that be your problem?
You need to change your query to:
#available_cars = Car_info.find_all_by_user_id(session[:user_id])
The find_all part will get you all records, whereas find only gets you the first. Another way to write this is:
#available_cars = Car_info.where("user_id = ?", session[:user_id])
Ideally, you want your class to be called CarInfo, not Car_info.

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])

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 :-)