简体   繁体   中英

Ruby on Rails detecting a user, but still returning an error that it doesn't exist

pretty new to RoR, but I'm using it to build a simple app and I'm running into an issue.

Basically my app uses three models: User, Post, and Thought. Where a User is a user, a Post is a post a user may make, and a Thought is like a comment on a Post.

I'm trying to get a post's thoughts to display below the post. On the post's show.html.erb page I have a call for <%= render @thoughts %> , which produces the _thought.html.erb template for each thought. The template looks like this:

<div>
    <li>
        <%= link_to thought.user.username, thought.user %>
        <span class="content"><%= thought.content %></span>
        <span class="timestamp"> Posted <%= time_ago_in_words(thought.created_at) %> ago.       </span>
    </li>
</div>

This previously worked, but now I added a form at the bottom and changed the ThoughtsController's create action a little bit. Now when I try to visit the post path I get this error: <%= link_to thought.user.username, thought.user %> - undefined method 'username' for nil:NilClass . I get this error even when I change the view to simply display the username, like such: <%= thought.user.username %>

I thought, fair enough, there's something wrong and the thoughts are probably not getting connected to a User anymore. When I tried to test that out, something weird happened. I changed the thought view to simply display " thought.inspect() ". The page then loaded fine, and the first thought was displayed like this:

<Thought id: 55, content: "Tenetur ut et sit nulla nesciunt modi eos.",
post_id: 295, user_id: 40, penny: false, created_at: "2013-08-26 21:37:55",
updated_at: "2013-08-26 21:37:55">

I thought, "hmm, this is weird. It seems to have a user_id. Maybe it's not connecting to the User itself. So I changed the post template to print out thought.user.inspect() . Now the first thought returns this:

#<User id: 40, username: "example_user39", created_at: "2013-08-26 21:37:41",
updated_at: "2013-08-26 21:37:41", password_digest:
"$2a$10$7urQB56QdkdXdNedsMe/KuENUTsQ.nk9FjKgLcE98sW6...",
remember_token: "336856d5e046b96983848f39d9e450aaa496252d", admin: false>

So I'm confused. When I try to print thought.user.username I get an error saying the Thought doesn't even have a User, but when I inspect them, I find out that the thought has a user and the user has a username. Am I missing something? What could be causing this? Where should I look? I can provide more info if necessary.

Thanks in advance!

Edit: Here's the server output:

Completed 500 Internal Server Error in 21ms
ActionView::Template::Error (undefined method 'username' for nil:NilClass):
    1: <div>
    2:  <li>
    3:          <%= link_to thought.user.username, thought.user %>
    4:          <span class="content"><%= thought.content %></span>
    5:          <span class="timestamp"> Posted <%= time_ago_in_words(thought.created_at) %> ago. </span>
    6:  </li>
  app/views/thoughts/_thought.html.erb:3:in '_app_views_thoughts__thought_html_erb___991787178796439758_70190024095460'
  app/views/posts/show.html.erb:10:in '_app_views_posts_show_html_erb__1664758191839643789_70190022800060'

Here's my Thought.rb:

class Thought < ActiveRecord::Base
  belongs_to :post
  belongs_to :user
  validates :user_id, presence: true
  validates :post_id, presence: true
  validates :content, presence: true
  default_scope -> { order('created_at DESC') }
end

I think, you build your thought for form in way like this:

@new_thought = @post.thoughts.build

After that, @post.thoughts includes @new_thought, too. And @new_thought.user is nil. That's why you get 500 error. You can modify your view:

<%- if thought.persisted? ->
 -- your code
<%- end -%

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM