Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning Devise current_user to comment

I was following following this online video (here) tutorial for a simple blog.

Inside these blog posts are comments.

In this tutorial the Devise gem was used and a migration was performed to assign add user_id to posts.

I did a migration of add_user_id_to_comments similar to was done in the user_id migration for posts.

class AddUserIdToComments < ActiveRecord::Migration
  def change
    add_column :comments, :user_id, :integer
    add_index :comments, :user_id
  end
end

My models contain the following:

Comment model

class Comment < ActiveRecord::Base
      belongs_to :post
      belongs_to :user
end

User model

class User < ActiveRecord::Base
  has_many :posts
  has_many :comments
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

I can assign a comment to a user doing the following.

user = User.first
comment = Comment.first
comment.user = user

This works in the console but the problem I'm running into is the comments_controller. I've tried to mimic the steps done with the post controller.

This is from the Post controller (which works fine)

    class PostsController < ApplicationController    
    before_action :find_post, only: [:show, :edit, :update, :destroy]       
    before_action :authenticate_user!, except: [:index, :show]    
    before_action :post_auth, only: [:edit, :update, :destroy]    

        def index    
            @posts = Post.all.order('created_at DESC')    
        end    

        def new    
            #@post = Post.new    
            @post = current_user.posts.build    
        end    

        def create    
            #@post = Post.new(post_params)    
            @post = current_user.posts.build(post_params)   

            if @post.save    
                redirect_to @post    
            else    
                render 'new'    
            end    
        end    

        def show    
        end    

        def edit    
        end    

        def update    
            if @post.update(params[:post].permit(:title, :body))    
                redirect_to @post    
            else   
                render 'edit'    
            end    
        end    

        def destroy    
            @post.destroy    
            redirect_to root_path    
        end    

        private    

        def post_params    
            params.require(:post).permit(:title, :body)    
        end   

        def find_post   
            @post = Post.find(params[:id])    
        end   

        # Checks if current user authored pin      
        def post_auth    
            if current_user != @post.user    
            redirect_to(root_path)    
            end    
        end    
    end

The Comments controller I'm having difficulty with

class CommentsController < ApplicationController

        before_action :find_comment, only: [:create, :destroy]    
        before_action :comment_auth, only: [:destroy]    

        def create    
            #@post = Post.find(params[:post_id])    
            #@comment = @post.comments.create(params[:comment].permit(:name, :body))    
            #redirect_to post_path(@post)

            #new format    
            @post = Post.find(params[:post_id])    
            #post = current_user.posts.build(post_params)    
            @comment = current_user.comments.build(comment_params)    
            redirect_to post_path(@post)    
        end   

        def destroy    
            @comment = @post.comments.find(params[:id]).destroy    
            redirect_to post_path(@post)   
        end    

        private    

        def comment_params    
            params.require(:comment).permit(:name, :body)    
        end   

        def find_comment    
            @post = Post.find(params[:post_id])    
        end   

        def comment_auth    
            if current_user != @post.user    
            redirect_to(root_path)   
            end    
        end     

    end

When I attempt to create a new comment; nothing happens...it redirects to the post page. There are no errors returned however.

Your thoughts how to resolve this issue? Any help will be appreciated.

EDIT w/ SOLUTION

I was running into issues where the the 'name and body values were not being passes to the database. After several attempts I finally was able to the name and body values passed with the following code:

def create
  @post = Post.find(params[:post_id])
  @comment = @post.comments.create(params[:comment].permit(:name, :body))
  @comment.user_id=current_user.id if current_user
  @comment.save

  if @comment.save
    redirect_to post_path(@post)
  else
    render 'new'
  end

Thank you all for the assistance

like image 333
captain awesome Avatar asked Mar 30 '26 20:03

captain awesome


1 Answers

@comment = current_user.comments.build(comment_params)

You're calling build on your comments and then not saving them. You either need to save or use create.

like image 111
jdl Avatar answered Apr 02 '26 11:04

jdl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!