在为用户设置了designgem之后,我如何才能在rails上创建帖子



我正试图在RubyonRails上构建一个基本的User/Post应用程序,一切都很顺利,但后来我添加了设计宝石来为我创建登录/注册,现在我无法创建帖子。一旦用户登录,他们将被重定向到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 

也许这可以帮助您…:

控制器内:

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

似乎对Post的某些验证失败了。我建议在你的控制器上做这样的事情:

def create 
@post = current_user.posts.build(post_params)
if @post.save!
redirect_to @post 
else
render 'new'
end
end

如果问题确实在验证中,save!将立即失败,并且您可以进一步调试它。

我在日志中看到请求已完成。有可能帖子真的被创建了?你检查数据库了吗?

最新更新