Rails回滚事务
Rails Rollback Transaction
我的 Rails 块有问题。在我实施评论部分后,我无法再创建帖子。控制台给了我一个回滚事务。所以我做了
p = Post.new
p.valid? # false
p.errors.messages
我的用户 :user=>["must exist"]
似乎有一些验证问题。但在我实施评论之前它确实有效。有人可以帮我吗?
User.rb
class User < ApplicationRecord
has_many :posts
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
Post.rb
class Post < ApplicationRecord
belongs_to :user
has_many :comments, dependent: :destroy
validates :title, presence: true, length: {minimum: 5}
validates :body, presence: true
has_attached_file :image #, :styles => { :medium => "300x300>", :thumb => "100x100>" }
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end
Post-迁移
class CreatePosts < ActiveRecord::Migration[5.1]
def change
create_table :posts do |t|
t.string :title
t.text :body
t.timestamps
end
end
end
Post_controller
class PostsController < ApplicationController
def index
@posts = Post.all.order("created_at DESC")
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 show
@post = Post.find(params[:id])
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update(post_params)
redirect_to @post
else
render 'edit'
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:title, :body, :theme)
end
end
当您创建 post 时,您需要在 post 控制器下的创建方法中为该 post 分配一个用户。你可以尝试这样的事情。
def create
if current_user
@post.user_id = current_user.id
end
## More create method stuff
end
默认情况下,在 belongs_to
关联中,用户需要创建 post,否则您将无法创建 post。因为,从外观上看,您没有任何东西可以在 create 方法中将用户分配给 post。
我的 Rails 块有问题。在我实施评论部分后,我无法再创建帖子。控制台给了我一个回滚事务。所以我做了
p = Post.new
p.valid? # false
p.errors.messages
我的用户 :user=>["must exist"]
似乎有一些验证问题。但在我实施评论之前它确实有效。有人可以帮我吗?
User.rb
class User < ApplicationRecord
has_many :posts
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
Post.rb
class Post < ApplicationRecord
belongs_to :user
has_many :comments, dependent: :destroy
validates :title, presence: true, length: {minimum: 5}
validates :body, presence: true
has_attached_file :image #, :styles => { :medium => "300x300>", :thumb => "100x100>" }
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end
Post-迁移
class CreatePosts < ActiveRecord::Migration[5.1]
def change
create_table :posts do |t|
t.string :title
t.text :body
t.timestamps
end
end
end
Post_controller
class PostsController < ApplicationController
def index
@posts = Post.all.order("created_at DESC")
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 show
@post = Post.find(params[:id])
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update(post_params)
redirect_to @post
else
render 'edit'
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:title, :body, :theme)
end
end
当您创建 post 时,您需要在 post 控制器下的创建方法中为该 post 分配一个用户。你可以尝试这样的事情。
def create
if current_user
@post.user_id = current_user.id
end
## More create method stuff
end
默认情况下,在 belongs_to
关联中,用户需要创建 post,否则您将无法创建 post。因为,从外观上看,您没有任何东西可以在 create 方法中将用户分配给 post。