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 On 6月7日, 下午5时06分, Colin Law <clan...@googlemail.com> wrote: > 2011/6/7 Jeweller <jiangna...@gmail.com>: > > 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 > > > > > > > On 6月7日, 下午2时50分, Frederick Cheung <frederick.che...@gmail.com> wrote: > >> On Jun 7, 3:37 am, Jeweller <jiangna...@gmail.com> wrote: > > >> > 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" > > >> 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 > > >> > On 6月7日, 上午2时29分, Frederick Cheung <frederick.che...@gmail.com> wrote: > > >> > > On Jun 6, 4:34 pm, Jeweller <jiangna...@gmail.com> 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. > > > -- > > 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+unsubscr...@googlegroups.com. > > For more options, visit this group > > athttp://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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.