title: Setting user_id for post creation in Rails
body: <p>I am having trouble setting the user_id on the creation of a post</p>
  
  <pre><code>def create
  
    @post = Post.new(post_params)
    @post.user_id = session[:id]
  
    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render action: 'show', status: :created, location: @post }
      else
        format.html { render action: 'new' }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end
  </code></pre>
  
  <p>The error I get is specifically <code>undefined method 'username' for nil:NilClass</code> when redirected to the show action. A look at the database shows that no user_id is set.</p>
  
  <p>I also tried passing the user_id as a hidden field. but this did not work either (a look at the logs showed the hidden field wasn't even being passed for some reason).</p>
  
  <p>Would appreciate a point in the right direction,</p>
  
  <p>Thank you !</p>
  
  <p>EDIT: as requested here is the show controller</p>
  
  <pre><code>def show
      @post = Post.find(params[:id])
  
      @original_id = params[:original_id]
  
      @comment = Comment.new
  end
  
  &lt;%= form_for [:admin, @post] do |f| %&gt;
    &lt;% if @post.errors.any? %&gt;
      &lt;div id="error_explanation"&gt;
        &lt;h2&gt;&lt;%= pluralize(@post.errors.count, "error") %&gt; prohibited this post from being saved:&lt;/h2&gt;
  
        &lt;ul&gt;
        &lt;% @post.errors.full_messages.each do |msg| %&gt;
          &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt;
        &lt;% end %&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
    &lt;% end %&gt;
  </code></pre>
  
  <p>Below is the form</p>
  
  <pre><code>  &lt;div class="field"&gt;
      &lt;%= f.label :title %&gt;&lt;br&gt;
      &lt;%= f.text_field :title %&gt;
    &lt;/div&gt;
    &lt;div class="field"&gt;
      &lt;%= f.label :content %&gt;&lt;br&gt;
      &lt;%= f.text_area :content %&gt;
    &lt;/div&gt;
    &lt;div class="field"&gt;
      &lt;%= f.label :category %&gt;&lt;br&gt;
      &lt;%= f.text_field :category %&gt;
    &lt;/div&gt;
    &lt;div class="actions"&gt;
      &lt;%= f.submit %&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>Routes:</p>
  
  <p>root 'posts#index'</p>
  
  <pre><code>  resources :posts do
      resources :comments, shallow: true
    end
  
    resources :users, only: [:create]
  
    #Posts
    get "/meta", to: "posts#meta"
  
    #User Routes
    get "/signup", to: "users#new"
    get "/success", to: "users#index"
  
    #login Routes
    get "/login", to: "sessions#login"
    post "/sessions/login_attempt", to: "sessions#login_attempt"
  
    #session routes
    get "/sessions/home", to: "sessions#home"
    get "/sessions/logout", to: "sessions#logout"
  
    #admin routes
    #resources :posts, module: 'admin', except: [:show, :index]
    namespace :admin do 
      root 'posts#new'
      resources :posts, except: [:show, :index]
    end
  </code></pre>