Friday, 15 March 2013

Rails beginner - comparison of Time with nil failed -



Rails beginner - comparison of Time with nil failed -

i'm working through blogger tutorial http://tutorials.jumpstartlab.com/projects/blogger.html#i3:-tagging

and maintain getting "comparison of time nil failed" error when add together

<p>posted <%=distance_of_time_in_words(comment.article.created_at, comment.created_at) %> later<p>

to comment's partial (_comments.html.erb)

here's schema

activerecord::schema.define(version: 20140618233420) create_table "articles", force: true |t| t.string "title" t.string "body" t.datetime "created_at" t.datetime "updated_at" end create_table "comments", force: true |t| t.string "author_name" t.text "body" t.integer "article_id" t.datetime "created_at" t.datetime "updated_at" end add_index "comments", ["article_id"], name: "index_comments_on_article_id" end

can explain? rails server indicates both comments , articles have timestamps how showing nil?

here total code in _comment.html.erb file

<div> <h4>comment <%= comment.author_name %></h4> <p class="comment"><%= comment.body %></p> <p>posted <%=distance_of_time_in_words(comment.article.created_at, comment.created_at) %> later<p> </div>

here's controller code:

class commentscontroller < applicationcontroller def create @comment = comment.new(comment_params) @comment.article_id = params[:article_id] @comment.save redirect_to article_path(@comment.article) end def comment_params params.require(:comment).permit(:author_name, :body) end end

make sure included in code:

<%= render partial: 'articles/comment', collection: @article.comments %>

/models/comment.rb

class comment < activerecord::base belongs_to :article end

ruby-on-rails

No comments:

Post a Comment