首页 > 解决方案 > 使用 devise gem 设置用户后,如何在 Rails 上创建帖子?

问题描述

我正在尝试在 Ruby on Rails 上构建一个基本的用户/帖子应用程序,一切运行顺利,但后来我添加了设计 gem 来为我创建登录/注册,现在我无法创建帖子。一旦用户登录,他们就会被重定向到 http://localhost:3000/posts。在那里,他们可以单击“添加新植物”(关于植物的帖子),然后将它们带到 http://localhost:3000/posts/new 页面。此处表单已启动并运行,用户可以填写标题和描述并点击创建帖子按钮,但它只是停留在此页面上,没有任何反应。帖子甚至没有保存。如果有人可以请给我一些有关如何尝试解决此问题的见解,我将不胜感激!在我使用设计添加用户表之前,这些帖子是正常创建的。

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)

这就是我点击创建帖子按钮时出现的内容。这是我的架构:

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

这是我的帖子控制器:

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 

标签: ruby-on-railsrubydeviserubygems

解决方案


也许这可以帮助你......:

在控制器中:

  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

在模型中

module User
  has_many :posts

  ...
end


module Post
  belongs_to :user

  ...
end

推荐阅读