lundi 30 novembre 2015

When I tried to create comment to micropost I have an error as "undefined method `comment_content'"

When I create a comment for micropost I have an error that "undefined method `comment_content'" and there is an problem on @comment.save method.

Please help me to fix the problem. Thank you for your attentions.

comments_controller

def create
  @micropost = Micropost.find(params[:micropost_id])
  @comment = Comment.new(comment_params)
  @comment.micropost = @micropost
  @comment.user = current_user
   if @comment.save
     flash[:success] = "Comment created!"
     redirect_to current_user
   else
     render 'shared/_comment_form'
   end
end

private

def comment_params
  params.require(:comment).permit(:content)
end

_comment_form

<%= form_for([micropost, @comment]) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
   <div class="field">
     <%= f.text_area :content %>
   </div>
    <button class="btn" type="submit">
      Create
    </button>
 <% end %>

comment.rb

belongs_to :micropost
belongs_to :user

validates :comment_content, presence: true
validates :user_id, presence: true
validates :micropost_id, presence: true 

static_pages_controller

 def home
  if logged_in?
  @micropost  = current_user.microposts.build
  @feed_items = current_user.feed.paginate(page: params[:page])
  @comment = Comment.new
end
end

_micropost.html.erb

 <%= render 'shared/comment_form', micropost: micropost %>

Aucun commentaire:

Enregistrer un commentaire