Ruby on Rails Monday, March 28, 2011


On Mon, Mar 28, 2011 at 4:02 PM, John Merlino <lists@ruby-forum.com> wrote:
Hey all,

I get the following error message:
ActionView::TemplateError (class or module required for rescue clause)
in app/vie$ app/helpers/format_helper.rb:116:in `divide_numbers'


Basically I have a field in database called Student Fails and I populate
fields with data. Sometimes the value can be empty - not null, not
integer, just empty because when user updates record they clear out
field and update it. First in my progressbar file I render a partial
called progressbar_item, passing in the string to the partial in render
method as well as data parameters that I can use in my progressbar file:


     = render "dashboard/progressbar_item.html", :actual =>
@student_counters[:student_failed], :expected =>
cu.context.expected_student_failed, :title => "Student Fails"

Now with the data parameters available in my view, I call the
divide_numbers method with the values of those parameters so if
expected_student_failed was an empty value in database, that empty value
gets passed as the second argument:

= render "dashboard/progressbar.html", :value =>
divide_numbers(actual.to_f, expected), :text => "#{actual} /
#{expected}"

Then the method is executed in format_helper:

def divide_numbers(x, y)
 result = x / y

This line will raise an error if y is nil or blank... It won't even get to the next line.
 
 result.to_s == 'NaN' ? 0 : result
rescue 0
end

If y is nothing (empty), the error occurs. If y is an integer the error
doesn't occur. Doesn't this line take all non numbers like empty and
convert it to integer 0:
result.to_s == 'NaN' ? 0 : result

If that's the case, then why do I get the error?

Will I be forced to do this (i haven't tested this yet but I presume it
would work):

def divide_numbers(x, y)
 result = x / y
 if result.nil?
  result.to_s = 0
 else
  result.to_s == 'NaN' ? 0 : result
 end
rescue 0
end


Thanks for response.

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment