Search code examples
ruby-on-railsrubydeviserubygems

How can I get post to be created on rails after setting up users with devise gem?


I am trying to build a basic User/Post app on Ruby on Rails and everything was working smoothly, but then I added the devise gem to create the login/sign up for me and now I am not able to create a post. Once the user signs in they are redirected to http://localhost:3000/posts. There they can click "add new plant" (its posts about plants) and it takes them to the http://localhost:3000/posts/new page. Here the form is up and running, the user can fill in the title and description and hit the create post button, but then it just stays on this page and nothing happens. The post doesnt even get saved. If anyone could please give me some insight on how I can attempt to fix this, I would really appreciate it! The posts were being created normally before I added the users table with devise.

Started POST "/posts" for ::1 at 2020-07-26 12:24:01 -0400
Processing by PostsController#create as HTML
  Parameters: {"authenticity_token"=>"Fm7HPgNsIJkKYzgOm/ApVCZfZJJuVQOLMP7eZvz1zFLok1QZdPxGC3f0p1Z1sRdUofCMlL5RU8wXwv31FIkj0w==", "post"=>{"title"=>"Lavender ", "description"=>"testing hello "}, "commit"=>"Create Post"}
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 1], ["LIMIT", 1]]
  Rendering posts/new.html.erb within layouts/application
  Rendered posts/_form.html.erb (Duration: 13.2ms | Allocations: 5423)
  Rendered posts/new.html.erb within layouts/application (Duration: 13.6ms | Allocations: 5487)
[Webpacker] Everything's up-to-date. Nothing to do
Completed 200 OK in 37ms (Views: 29.1ms | ActiveRecord: 0.2ms | Allocations: 11827)

that is the what shows up when I hit the create post button. Here is my schema:

ActiveRecord::Schema.define(version: 2020_07_26_023022) do

  create_table "posts", force: :cascade do |t|
    t.string "title"
    t.text "description"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.integer "user_id"
  end

  create_table "users", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

end

Here is my posts controller:

class PostsController < ApplicationController
    before_action :find_post, only: [:show, :edit, :update, :destroy]

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

    def show
    end

    def new 
        @post = Post.new
    end

    def create 
        @post = Post.new(post_params)
        if @post.save
            redirect_to @post 
        else
            render 'new'
        end
    end

    def edit 
    end

    def update 
        if @post.update(post_params)
            redirect_to @post
        else 
            rend 'new'
        end

    end

    def destroy 
        @post.destroy
        redirect_to posts_path
    end

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

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

Solution

  • Maybe this can help You...:

    In controller:

      def create 
        @post = Post.new(post_params)
        @post.user = current_user # here You setting the user
    
        if @post.save
          redirect_to @post 
        else
          puts @post.errors # this should print errors to the console  
          render 'new'
        end
      end
    

    In models

    module User
      has_many :posts
    
      ...
    end
    
    
    module Post
      belongs_to :user
    
      ...
    end