Need help with an awkard error

Hi, everyone. Need help here.
the basic relationship is that a user has many microposts and
comments, a micropost has many comments.
What I want to do is to show a commenter’s name in the brower.

Here’s the code:

pagesController.rb

the “feed” method is to retrun some reletive microposts

    @feed_items=current_user.feed.paginate(:page=>params[:page])

home.html.erb

<%= render :partial=>‘shared/feed_item’, :collection=>@feed_items
%>

_feed_item.html.erb

<% for comment in feed_item.comments %>
<% comment.commenter.name %> # this line cause
the error, but <% comment.commenter %> is ok!
<% end %>

there’s an error " Error during failsafe response: incompatible
encoding regexp match (UTF-8 regexp with ASCII-8BIT string)"
rror during failsafe response: incompatible encoding regexp match
(UTF-8 regexp with ASCII-8BIT string)
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.0.5/lib/rails/
backtrace_cleaner.rb:10:in sub' /usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.0.5/lib/rails/ backtrace_cleaner.rb:10:inblock in initialize’
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/
active_support/backtrace_cleaner.rb:72:in call' /usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/ active_support/backtrace_cleaner.rb:72:inblock (2 levels) in filter’
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/
active_support/backtrace_cleaner.rb:72:in map' /usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/ active_support/backtrace_cleaner.rb:72:inblock in filter’
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/
active_support/backtrace_cleaner.rb:71:in each' /usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/ active_support/backtrace_cleaner.rb:71:infilter’
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/
active_support/backtrace_cleaner.rb:26:in clean' /usr/local/ruby/lib/ruby/gems/1.9.1/gems/actionpack-3.0.5/lib/ action_dispatch/middleware/show_exceptions.rb:150:inclean_backtrace’
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/actionpack-3.0.5/lib/
action_dispatch/middleware/show_exceptions.rb:137:in
application_trace' /usr/local/ruby/lib/ruby/gems/1.9.1/gems/actionpack-3.0.5/lib/ action_dispatch/middleware/show_exceptions.rb:131:inblock in
log_error’
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/
active_support/deprecation/reporting.rb:16:in silence' /usr/local/ruby/lib/ruby/gems/1.9.1/gems/actionpack-3.0.5/lib/ action_dispatch/middleware/show_exceptions.rb:128:inlog_error’
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/actionpack-3.0.5/lib/
action_dispatch/middleware/show_exceptions.rb:64:in render_exception' /usr/local/ruby/lib/ruby/gems/1.9.1/gems/actionpack-3.0.5/lib/ action_dispatch/middleware/show_exceptions.rb:59:incall’
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.0.5/lib/rails/
rack/logger.rb:13:in call' /usr/local/ruby/lib/ruby/gems/1.9.1/gems/rack-1.2.3/lib/rack/ runtime.rb:17:incall’
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/
active_support/cache/strategy/local_cache.rb:72:in call' /usr/local/ruby/lib/ruby/gems/1.9.1/gems/rack-1.2.3/lib/rack/lock.rb: 11:inblock in call’
internal:prelude:10:in synchronize' /usr/local/ruby/lib/ruby/gems/1.9.1/gems/rack-1.2.3/lib/rack/lock.rb: 11:incall’
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/actionpack-3.0.5/lib/
action_dispatch/middleware/static.rb:30:in call' /usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.0.5/lib/rails/ application.rb:168:incall’
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.0.5/lib/rails/
application.rb:77:in method_missing' /usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.0.5/lib/rails/ rack/log_tailer.rb:14:incall’
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/rack-1.2.3/lib/rack/
content_length.rb:13:in call' /usr/local/ruby/lib/ruby/gems/1.9.1/gems/rack-1.2.3/lib/rack/handler/ webrick.rb:52:inservice’
/usr/local/ruby/lib/ruby/1.9.1/webrick/httpserver.rb:111:in
service' /usr/local/ruby/lib/ruby/1.9.1/webrick/httpserver.rb:70:inrun’
/usr/local/ruby/lib/ruby/1.9.1/webrick/server.rb:183:in `block in
start_thread’

so I placed this line in the home.html.erb to test
<%= @feed_items.first.comments.first.commenter.name %>
it’s worked!

I’m very confused. I mean what’s the difference between these two
ways? And also the error message, it’s very strange. because I’ve
specified utf-8 encoding in the database.yml

On Jun 6, 4:34pm, Jeweller [email protected] wrote:

so I placed this line in the home.html.erb to test
<%= @feed_items.first.comments.first.commenter.name %>
it’s worked!

I’m very confused. I mean what’s the difference between these two
ways? And also the error message, it’s very strange. because I’ve
specified utf-8 encoding in the database.yml

Well the main difference is that this is only displaying a single
name. If for example there is a comment where comment.commenter is nil
then calling .name on it would raise an error but your second example
would be ok unless the bad comment happens to be the first comment of
the first item.

I think the actual error message you get is something unrelated
happening during the process of preparing the error page. Does the
path to your rails app contain some non ASCII characters perhaps?

Fred.

thanks ,Fred.
I still don’t understand,because in the model I use the
“validates_presense_of :commenter” to make sure the commenter
won’t be nil.
Here’s the file I used to generate sample data.

require ‘faker’
def make_comments
Micropost.all.each do |micropost|
User.all(:limit => 2).each do |user|
micropost.comments.create!(:content =>
Faker::Lorem.sentence(4), :commenter => user)
end
end
end

Now, every micropost has two comments, and the commenters are the
first,and the second one in the database.

In your opinon, what may brings to the situation —" the path to your
rails app contain some non ASCII characters"

Try adding the following to the top of your view:

<% # coding: UTF-8 %>

On Jun 7, 3:37 am, Jeweller [email protected] wrote:

  micropost.comments.create!(:content =>

Faker::Lorem.sentence(4), :commenter => user)
end
end
end

Now, every micropost has two comments, and the commenters are the
first,and the second one in the database.

In your opinon, what may brings to the situation —" the path to your
rails app contain some non ASCII characters"

I don’t think it’s related to the actual problem in your app but I do
think it could be triggering a bug in the backtrace cleaner stuff in
rails, which is obscuring the actual error you get. Should be easy
enough to test this hypothesis

Fred

Sorry, I don’t know how to test it. I’m new to Rails. Could you
specify it?
Or let’s say if you want a user able to comment on microposts and also
comments, what would you do?
I just don’t want to use “act_as_commentable” gem, because I’m trying
to learn

2011/6/7 Jeweller [email protected]:

Could you not top post please, insert your reply at appropriate points
in the previous post, that makes it easier to follow the thread.

Sorry, I don’t know how to test it. I’m new to Rails. Could you
specify it?
Or let’s say if you want a user able to comment on microposts and also
comments, what would you do?
I just don’t want to use “act_as_commentable” gem, because I’m trying
to learn

I think Frederick is suggesting that there may be some unusual
characters in the path to your app that is causing the unusual error
messages. What is the path to the app (something like
/home/jeweller/myapp for example)?

If there are such characters then the easy test to see if this is the
problem is to copy the entire app to a different location and run it
there (or just rename folders to ascii text).

Colin

2011/6/7 Jeweller [email protected]:

Well, I think Fred may be right. The path to my app contains some
Chinese, like “桌面”. So I removed it to another directory, but there
came with a new error “undefined method `name’ for nil:NilClass”
instead of the old one.

As I asked previously, don’t top post. Thanks.

comment.commenter is a nilclass !!! but <%= comment.commenter %> it
got #User:0xa86725c

I think you must be mistaken, it cannot both be nil and User at the
same time. As fred suggested, maybe some are ok and others are nil.
Try using something like
<%= comment.commenter.name if comment.commenter %>
Also have a look at the Rails Guide on debugging. It will show you
how to use ruby-debug to break into your code and inspect data and
follow flow.

Colin

Well, I think Fred may be right. The path to my app contains some
Chinese, like “”. So I removed it to another directory, but there
came with a new error “undefined method `name’ for nil:NilClass”
instead of the old one.

comment.commenter is a nilclass !!! but <%= comment.commenter %> it
got #User:0xa86725c

here is the model,there may be some problem.

class User < ActiveRecord::Base
has_many :comments, :dependent => :destory
#…
end

class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
belongs_to :micropost
belongs_to :commenter, :class_name => “User”, :foreign_key =>
“commenter_id”
has_many :comments, :as => :commentable, :dependent => :destroy

A comment can also be commented

#…
end

Thanks Colin and Fred,it works with<%= comment.commenter.name if
comment.commenter %>
I still don’t understand why some of the commenters are nil, but I
will take your advise and learn how to debug myself.
Thanks very much!!