使用已创建的 object 保存当前用户
Save current user with created object
我在使用 rails 应用中创建的 object 保存当前用户时遇到问题。
我遵循 Michael Hartl rails 教程直到第 10 章(我不需要第 11 到 14 章的以下功能。)从那里开始我为 children 和幼儿园生成了一些脚手架并编辑模型中的关系(例如,用户有多个 children,child 属于用户)。目的是获得一个应用程序,如果用户被分配了 parents 角色,用户可以创建他或她的 child 或 children,或者用户可以创建幼儿园,如果用户被分配了 kindergarten-manager 角色。之后,该应用程序应有助于将所有已注册的 children 分配给所有已注册的幼儿园名额。
我目前的问题是用户(此时没有角色,只是普通用户)无法在 Web 界面中创建 child,因为它在尝试保存时显示 "user must exist" child,因为我想没有用户分配给 child。
不幸的是,我不知道如何将当前用户保存到 child。我发现了一个非常相似的问题 并且我尝试按照答案进行操作,但我无法解决问题,而是现在出现错误。
我将 .merge(user: current_user)
部分编辑到 child 控制器,但它给了我错误:
"ActiveModel::MissingAttributeError in ChildrenController#create
无法写入未知属性 user_id
"
# POST /children.json
def create
@child = Child.new (child_params)
@child.save
型号user.rb和child.rb:
#app/models/child.rb
class Child < ApplicationRecord
belongs_to :user
validates :user, presence: true
end
#app/models/user.rb
class User < ApplicationRecord
attr_accessor :remember_token, :activation_token, :reset_token
has_many :children, dependent: :destroy
has_many :kindergartens, dependent: :destroy
....
end
children_controller.rb:
#app/controllers/children_controller.rb
class ChildrenController < ApplicationController
before_action :set_child, only: [:show, :edit, :update, :destroy]
....
def create
@child = Child.new child_params
@child.save
respond_to do |format|
if @child.save
format.html { redirect_to @child, notice: 'Child was successfully created.' }
format.json { render :show, status: :created, location: @child }
else
format.html { render :new }
format.json { render json: @child.errors, status: :unprocessable_entity }
end
end
end
....
def child_params
params.require(:child).permit(:firstname, :lastname, :postalcode, :city, :street, :addr_number, :gender, :disability, :allday, :halal, :koscher, :vegetarian, :vegan).merge(user: current_user)
end
end
首先你需要添加 user_id
索引到 childrens
table.
然后
如果您有登录和注销功能,我的意思是会话当前正在运行,那么您如何管理用户会话?是这样的吗?
def current_user
@current_user ||= User.find_by(id: session[:user_id])
end
如果是,则转到 create
方法并按如下方式编辑
@child = Child.new child_params
@child.user = current_user
@child.save
并从 child_params
中删除 .merge(user: current_user)
,我希望它会起作用。
如果上述解决方案无法正常工作,请使用
中的形式手动传递 user_id
<%= f.hidden_field :user_id, value: current_user.id %>
然后user_id
像其他属性一样添加到strong参数。
您可以看到 user_id
正常通过测试,然后
发生了什么
仅供测试
@child = Child.new child_params
@child.user = User.last
@child.save
第 2 部分
如果您需要授予 children_controller
权限,例如如果用户具有 parent,则 he/she 可以访问 children_controller
表单,然后创建这样的方法
before_action :require_parants, only: [:new, :create] # top of the controller
private
# parant column on the users table is boolean true/false
def require_parants
if !logged_in? || (logged_in? and !current_user.parant?)
flash[:danger] = "Only parants can create child"
redirect_to root_url
end
end
logged_in?
基于此
# Returns true if the user is logged in, false otherwise.
def logged_in?
!current_user.nil?
end
然后当用户不是 parent.
时重定向到根目录 URL
希望对您有所帮助
我在使用 rails 应用中创建的 object 保存当前用户时遇到问题。
我遵循 Michael Hartl rails 教程直到第 10 章(我不需要第 11 到 14 章的以下功能。)从那里开始我为 children 和幼儿园生成了一些脚手架并编辑模型中的关系(例如,用户有多个 children,child 属于用户)。目的是获得一个应用程序,如果用户被分配了 parents 角色,用户可以创建他或她的 child 或 children,或者用户可以创建幼儿园,如果用户被分配了 kindergarten-manager 角色。之后,该应用程序应有助于将所有已注册的 children 分配给所有已注册的幼儿园名额。
我目前的问题是用户(此时没有角色,只是普通用户)无法在 Web 界面中创建 child,因为它在尝试保存时显示 "user must exist" child,因为我想没有用户分配给 child。
不幸的是,我不知道如何将当前用户保存到 child。我发现了一个非常相似的问题 .merge(user: current_user)
部分编辑到 child 控制器,但它给了我错误:
"ActiveModel::MissingAttributeError in ChildrenController#create
无法写入未知属性 user_id
"
# POST /children.json
def create
@child = Child.new (child_params)
@child.save
型号user.rb和child.rb:
#app/models/child.rb
class Child < ApplicationRecord
belongs_to :user
validates :user, presence: true
end
#app/models/user.rb
class User < ApplicationRecord
attr_accessor :remember_token, :activation_token, :reset_token
has_many :children, dependent: :destroy
has_many :kindergartens, dependent: :destroy
....
end
children_controller.rb:
#app/controllers/children_controller.rb
class ChildrenController < ApplicationController
before_action :set_child, only: [:show, :edit, :update, :destroy]
....
def create
@child = Child.new child_params
@child.save
respond_to do |format|
if @child.save
format.html { redirect_to @child, notice: 'Child was successfully created.' }
format.json { render :show, status: :created, location: @child }
else
format.html { render :new }
format.json { render json: @child.errors, status: :unprocessable_entity }
end
end
end
....
def child_params
params.require(:child).permit(:firstname, :lastname, :postalcode, :city, :street, :addr_number, :gender, :disability, :allday, :halal, :koscher, :vegetarian, :vegan).merge(user: current_user)
end
end
首先你需要添加 user_id
索引到 childrens
table.
然后
如果您有登录和注销功能,我的意思是会话当前正在运行,那么您如何管理用户会话?是这样的吗?
def current_user
@current_user ||= User.find_by(id: session[:user_id])
end
如果是,则转到 create
方法并按如下方式编辑
@child = Child.new child_params
@child.user = current_user
@child.save
并从 child_params
中删除 .merge(user: current_user)
,我希望它会起作用。
如果上述解决方案无法正常工作,请使用
中的形式手动传递user_id
<%= f.hidden_field :user_id, value: current_user.id %>
然后user_id
像其他属性一样添加到strong参数。
您可以看到 user_id
正常通过测试,然后
仅供测试
@child = Child.new child_params
@child.user = User.last
@child.save
第 2 部分
如果您需要授予 children_controller
权限,例如如果用户具有 parent,则 he/she 可以访问 children_controller
表单,然后创建这样的方法
before_action :require_parants, only: [:new, :create] # top of the controller
private
# parant column on the users table is boolean true/false
def require_parants
if !logged_in? || (logged_in? and !current_user.parant?)
flash[:danger] = "Only parants can create child"
redirect_to root_url
end
end
logged_in?
基于此
# Returns true if the user is logged in, false otherwise.
def logged_in?
!current_user.nil?
end
然后当用户不是 parent.
时重定向到根目录 URL希望对您有所帮助