title: basic Ruby on Rails undefined method issue
body: <p>I'm a relative amateur to RoR developing (about a month's experience) and I've been referencing variables from different controllers with no issues up until this point. I'm developing a CMS for a blog and I cannot get the blog's author's username to appear on the index or show pages. I receive a "undefined method `username' for nil:NilClass" error. However, I can get the ID. Can anyone help point out the errors in my code? I would greatly appreciate it.</p>
  
  <p>This is the blog model:</p>
  
  <pre><code>    class Blog &lt; ActiveRecord::Base
      attr_accessible :post, :title
  
      has_one :users
      belongs_to :user
  
          end
  </code></pre>
  
  <p>This is the blog controller's show and index section:</p>
  
  <pre><code>    def index
        @users = User.all
        @blogs = Blog.all
  
  
       respond_to do |format|
         format.html # index.html.erb
         format.json { render json: @blogs }
         end
           end
  
         # GET /blogs/1
         # GET /blogs/1.json
     def show
       @blog = Blog.find(params[:id])
       @users = User.all      
  
           respond_to do |format|
             format.html # show.html.erb
             format.json { render json: @blog }
           end
        end
  </code></pre>
  
  <p>Here is the index.html.erb code:</p>
  
  <pre><code>     &lt;% @blogs.each do |blog| %&gt;
         &lt;tr&gt;
          &lt;td&gt;&lt;%= blog.title %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= blog.post %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= blog.user.username %&gt;&lt;/td&gt; #blog.user_id works perfectly here.
          &lt;td&gt;&lt;%= link_to 'Show', blog %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= link_to 'Edit', edit_blog_path(blog) %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= link_to 'Destroy', blog, method: :delete, data: { confirm:'sure?'} %&gt;&lt;/td&gt;
         &lt;/tr&gt;
           &lt;% end %&gt;
  </code></pre>
  
  <p>This is the code for the show.html.erb file:</p>
  
  <pre><code>      &lt;p&gt;
        &lt;b&gt;Title:&lt;/b&gt;
         &lt;%= @blog.title %&gt;
         &lt;/p&gt;
  
         &lt;p&gt;
         &lt;b&gt;Author:&lt;/b&gt;
          &lt;%= @blog.user.username %&gt;
         &lt;/p&gt;
  
         &lt;p&gt;
         &lt;b&gt;Post:&lt;/b&gt;
         &lt;%= @blog.post %&gt;
         &lt;/p&gt;
  </code></pre>
  
  <p>Here is the create code in the blog controller:</p>
  
  <pre><code>      Here is the create code:
  
      # POST /blogs
      # POST /blogs.json
          def create
           @blog = Blog.new(params[:blog])
           @blog.user_id = current_user
  
             respond_to do |format|
              if @blog.save
                format.html { redirect_to @blog, notice: 'Blog was successfully created.' }
                format.json { render json: @blog, status: :created, location: @blog }
                 else
                  format.html { render action: "new" }
                  format.json { render json: @blog.errors, status: :unprocessable_entity }
                end
              end
            end
  </code></pre>