Rails heroku error for nil - ruby-on-rails-3

My app runs fine on my iMac in development.
But, I get the following error on Heroku (this is the Heroku log):
ActionView::Template::Error (undefined method `name' for nil:NilClass):
22: <% end %>
23: <td><%= workorder.description %></td>
24: <% if workorder.location_id != nil %>
25: <td><%= workorder.location.name %></td>
app/views/home/_myopenorders.html.erb:25:in `block in _app_views_home__myopenorders_html_erb__1134835514768073858_64792180'
app/views/home/index.html.erb:66:in `_app_views_home_index_html_erb___3327722386939287121_60092520'
26: <% else %>
27: <td></td>
app/views/home/_myopenorders.html.erb:15:in `_app_views_home__myopenorders_html_erb__1134835514768073858_64792180'
28: <% end %>
This is the code:
<% if workorder.location_id != nil %>
<td><%= workorder.location.name %></td>
<% else %>
<td></td>
<% end %>
Any ideas?
UPDATE:
Sometimes the log looks like Heroku is running the ruby code out of sequence. Look at this:
ActionView::Template::Error (undefined method `name' for nil:NilClass):
22: <% end %>
26: <% else %>
app/views/home/index.html.erb:66:in `_app_views_home_index_html_erb__2268065945584360823_49285320'
app/views/home/_myopenorders.html.erb:25:in `block in _app_views_home__myopenorders_html_erb___3485426105947531181_51160960'
27: <td></td>
app/views/home/_myopenorders.html.erb:15:in `_app_views_home__myopenorders_html_erb___3485426105947531181_51160960'
28: <% end %>
24: <% if workorder.location_id != nil %>

Sorry, my fault. A location got deleted and I didn't have the code to check whether the location was used by a workorder.

Related

NoMethodError in Store#index

I am getting the following error, please any one view it and help me out of this..
Actually this is the error shown in my development.log file.
ActionView::Template::Error (undefined method `quantity' for #<LineItem:0xaa47b1c>):
4: <tr>
5: <% end %>
6: <tr>
7: <td><%= line_item.quantity %>×</td>
8: <td><%= line_item.product.title %></td>
9: <td class="item_price"><%= number_to_currency(line_item.total_price) %></td>
10: </tr>
app/views/line_items/_line_item.html.erb:7:in `_app_views_line_items__line_item_html_erb___826332857_89103730'
app/views/carts/_cart.html.erb:4:in `_app_views_carts__cart_html_erb___185026047_87767060'
app/views/layouts/application.html.erb:19:in `_app_views_layouts_application_html_erb___370633105_87583010'
app/helpers/application_helper.rb:6:in `hidden_div_if'
app/views/layouts/application.html.erb:18:in `_app_views_layouts_application_html_erb___370633105_87583010'
The error's quite clear: your LineItem class does not have a method (or database field) called quantity.

How can I fix this error: "incompatible character encodings: UTF-8 and ASCII-8BIT"?

I am trying to use the rmmseg-cpp gem's sample code documented here: http://rmmseg-cpp.rubyforge.org/#Stand-Alone-rmmseg
Just to test it out I put it in show.html.erb like this:
# coding: UTF-8
<p id="notice"><%= notice %></p>
<p>
<b>Title:</b>
<%= #lesson.title %>
</p>
<p>
<b>Content:</b>
<%= #lesson.content %> # simplified chinese text
</p>
<p><% require 'rmmseg' %>
<% algor = RMMSeg::Algorithm.new(#lesson.content) %>
<% loop do %>
<% tok = algor.next_token %>
<% break if tok.nil? %>
<%= "#{tok.text} [#{tok.start}..#{tok.end}]" %>
<% end %> </p>
<%= link_to 'Edit', edit_lesson_path(#lesson) %> |
<%= link_to 'Back', lessons_path %>
I get the following error:
Encoding::CompatibilityError in Lessons#show
Showing /Users/webmagnets/rails_projects/blt/app/views/lessons/show.html.erb where line #19 raised:
incompatible character encodings: UTF-8 and ASCII-8BIT
Extracted source (around line #19):
16: <% loop do %>
17: <% tok = algor.next_token %>
18: <% break if tok.nil? %>
19: <%= "#{tok.text} [#{tok.start}..#{tok.end}]" %>
20: <% end %> </p>
21:
22: <%= link_to 'Edit', edit_lesson_path(#lesson) %> |
Rails.root: /Users/webmagnets/rails_projects/blt
Application Trace | Framework Trace | Full Trace
app/views/lessons/show.html.erb:19:in `block in _app_views_lessons_show_html_erb___3831310028264182552_70339844987120'
app/views/lessons/show.html.erb:16:in `loop'
app/views/lessons/show.html.erb:16:in `_app_views_lessons_show_html_erb___3831310028264182552_70339844987120'
app/controllers/lessons_controller.rb:20:in `show'
Request
Parameters:
{"id"=>"1"}
Show session dump
Show env dump
Response
Headers:
None
If you need any more info, please let me know.
This link helped me: https://github.com/sinatra/sinatra/issues/559#issuecomment-7748296
I used <% text = tok.text.force_encoding 'UTF-8' %> and it worked.
Thanks #zed_0xff for putting me on the right path.
try this workaround
<% text = tok.text.encode('utf-8',:invalid => :replace, :undef => :replace) %>
<%= "#{text} [#{tok.start}..#{tok.end}]" %>

Rails: Instance of IO needed

I have this error in my Rails app:
ActionView::Template::Error (instance of IO needed):
30: <tr>
31: <% #product.images.each do |img| %>
32: <td id="product_image_<%= img.id.to_s %>">
33: <%= image_tag img.miniature %><br />
34: <%= link_to_remote raw(t(:delete)), :url => { :action => :de
lete_image, :id => img.id }, :update => "product_image_#{img.id.to_s}" %>
35: </td>
36: <% end %>
app/models/image.rb:45:in `formats_from_yaml'
img.miniature looks like this:
Images::formats.each_key do |name|
define_method(name) do
self.formats_from_yaml[name][:url]
end
end
Images::formats is a hash table of format names as a string, width and height in pixels and a quality for the image-jpg.
What's the mistake?
And formats_from_yaml like this:
def formats_from_yaml
YAML.load(self.formats)
end
self.formats is a hash with the name of the format and the url of the image. edit: self.formats is the hash as yaml.
What confuses me is that if I call Image.first.formats_from_yaml by the rails console, it will work.
What's the mistake?
YAML.load takes an IO stream or string as a parameter and loads the document from it. If self.formats is a Hash, why pass it to YAML.load? It seems like the block should be:
define_method(name) do
self.formats[name][:url]
end

Why can't I access this field? (Rails 3)

I really can't figure out why I can't access the user_id field for the class Post. I have it as a field in my database (I can see it, and it isn't blank. Yet for some reason, I'm getting this error:
undefined method `user_id' for #<Class:0x103497448>
Extracted source (around line #10):
7: <h2>Topics</h2>
8: <% #board.topics.each do |topic| %>
9: <% #post = topic.posts(1) %>
10: <b><%= User.find(#post.user_id).username %> says <%= link_to topic.subject, [#board, topic] %></b><br />
11: <% end %>
12: <% end %>
13:
topic.posts(1) will return an array. Hence #<Class:0x103497448>
The right way to get the first post for a topic would be
9: <% #post = topic.posts.first %>
10: <b><%= User.find(#post.user_id).username %> says <%= link_to topic.subject, [#board, topic] %></b><br />
Try <% #post = topic.posts.limit(1) %>

show_for gem gives to_key error

hi i am using show_for gem and it gives me error like: undefined method `to_key' for # in app/views/messages/index.html.erb where line #46 raised:
my view look like these:
46: <%= show_for #messages do |s| %>
47: <%= s.attribute :subject %>
48: <%= s.attribute :message %>
49: <% end %>
my controller code look like:
#messages= Message.all
please help me what i am doing wrong. thanks in advance
I do not know this gem but according to its documentation, show_for is not supposed to receive a collection as argument (#messages) but a single object. I might be wrong.
With this supposition, I would to :
<% #messages.each do |message| %>
<%= show_for message do |s| %>
<%= s.attribute :subject %>
<%= s.attribute :message %>
<% end %>
<% end %>