ruby - Rails App - Object_ID Returning for User Name -
i developing simple blog app , having trouble displaying user name associated comment.
comments belongs_to :posts , :users. posts belongs_to :user , has_many :comments. users has_many :posts , :comments.
the create action in comments model works , stores comments expected post_id , user_id:
class commentscontroller < applicationcontroller def create @post = post.find(params[:post_id]) @comment = @post.comments.create(comment_params) @comment.user = current_user if @comment.save redirect_to @post else flash.now[:danger] = "error" end end private def comment_params params.require(:comment).permit(:content) end end
i able access user name via console expected comment.first.user.name returns users name. when trying in view, following error:
undefined method `name' nil:nilclass
here view:
<% @post.comments.each |comment| %> <p>user:<%= comment.user.name %></p> <p>comment:<%= comment.content %></p> <% end %>
when remove .name user in view, app displays looks object_id.
user:#<user:0x00000001f62830> comment:test comment
i've tried resetting database mentioned here: show username when posting comments in rails
i've tried address proxy_id mentioned here: returning issue in comment::activerecord_associations_collection
i'm not sure else try. also, when reset comments table there no data in it, app still displays:
user: comment:
when loops through though there no data in it. think has dynamic finder confused id, tried moving controller mentioned here, retrieving username find_by_id(comment.user_id).name not working, , still getting undefined method errors. ideas? appreciate insight. not using comments gem, developing in cloud9, , using postgresql.
i found issue in post: using <%= render comments %> outputs empty partial, if there nothing in database
"the issue form above <%= render @post.comments %> there empty comment when reach partial comments, if there's none in database, put <%= render "comments/form" %> below fix this."
Comments
Post a Comment