mardi 5 mai 2020

undefined method `id' for nil:NilClass in rails

I am creating instagram app from https://medium.com/luanotes/build-instagram-by-ruby-on-rails-part-2-d70b44f5c7e6.

Under this topic: Add a form to create a new Post in Homepage

I am getting this error : undefined methodid' for nil:NilClass`

my index view file

<%= form_for Post.new do |f| %>
  <div class="form-group">
    <%= f.text_field :description %>
  </div>
  <div class="form-group">
    <%= f.file_field :image %>
  </div>
  <div class="form-group">
    <%= f.text_field :user_id,nil, value: current_user.id, class:'d-none'%>
  </div>
  <br>
  <div class="text-center">
    <%= f.submit 'Create Post', class: 'btn btn-primary' %>
  </div>
<% end %>

Controller file:

class PostsController < ApplicationController
    def create
        Post.create(post_params)

        redirect_to root_path
    end

    private

    def post_params
        params.require(:post).permit(:description, :image, :user_id)
    end
end

Aucun commentaire:

Enregistrer un commentaire