使用设计 gem 设置用户后,如何在 rails 上创建 post?
How can I get post to be created on rails after setting up users with devise gem?
我正在尝试在 Rails 的 Ruby 上构建一个基本的 User/Post 应用程序,一切都运行顺利,但后来我添加了设计 gem 来创建login/sign 适合我,现在我无法创建 post。用户登录后,他们将被重定向到 http://localhost:3000/posts。他们可以在此处单击“添加新植物”(它的 post 是关于植物的)并将它们带到 http://localhost:3000/posts/new 页面。此处的表单已启动 运行,用户可以填写标题和说明并点击创建 post 按钮,但随后它就停留在该页面上,没有任何反应。 post 甚至没有被保存。如果有人可以给我一些关于如何尝试解决此问题的见解,我将不胜感激! post 在我使用 devise 添加用户 table 之前正常创建。
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)
这就是我点击创建 post 按钮时出现的内容。这是我的架构:
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
这是我的 post 控制器:
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
也许这可以帮助你...:[=12=]
在控制器中:
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!
如果问题确实在验证中,将立即失败,您将能够进一步调试它。
或
我在日志中看到请求已完成。帖子实际上可能正在创建吗?你检查过你的数据库了吗?
我正在尝试在 Rails 的 Ruby 上构建一个基本的 User/Post 应用程序,一切都运行顺利,但后来我添加了设计 gem 来创建login/sign 适合我,现在我无法创建 post。用户登录后,他们将被重定向到 http://localhost:3000/posts。他们可以在此处单击“添加新植物”(它的 post 是关于植物的)并将它们带到 http://localhost:3000/posts/new 页面。此处的表单已启动 运行,用户可以填写标题和说明并点击创建 post 按钮,但随后它就停留在该页面上,没有任何反应。 post 甚至没有被保存。如果有人可以给我一些关于如何尝试解决此问题的见解,我将不胜感激! post 在我使用 devise 添加用户 table 之前正常创建。
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)
这就是我点击创建 post 按钮时出现的内容。这是我的架构:
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
这是我的 post 控制器:
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
也许这可以帮助你...:[=12=]
在控制器中:
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!
如果问题确实在验证中,将立即失败,您将能够进一步调试它。
或
我在日志中看到请求已完成。帖子实际上可能正在创建吗?你检查过你的数据库了吗?