我正在一个网站上工作,用户可以输入小时数,主管可以批准或拒绝他们,但是我不断收到此错误: 不允许的参数::supervisor
我尝试过更改has_many关系,像Nested attributes unpermitted parameters所说的那样更改管理程序的参数,但无济于事。
我的hours_controller.rb(我在这里遇到错误)
def create
@hour = current_user.hours.build(hour_params)
@supervisor = Supervisor.find_by(params[:email])
if @hour.save
flash[:info] = "Waiting for approve"
redirect_to current_user
end
end
private
def hour_params
params.require(:hour).permit(:content, :approved)
end
我的小时.rb
class Hour < ApplicationRecord
belongs_to :user
belongs_to :supervisor
default_scope -> { order(created_at: :desc) }
validates :user_id, presence: true
validates :content, :inclusion => 1..100
validates :approved, presence: true, default: false
end
我的supervisor_controller.rb
class SupervisorsController < ApplicationController
def new
@supervisor = Supervisor.new
end
def create
Supervisor.new = Supervior.create(supervisor_params)
if @supervisor.save
flash[:info] = "Please check your email to activate your account"
redirect_to root_url
else
render 'new'
end
end
def home
end
def hours
end
private
def supervisor_params
params.require(:supervisor).permit(:id, :first_name, :last_name, :email, :address, :telephone, :role, :organization, :password, :password_confirmation, :signature, { hour: [:content, :approved]})
end
end
我的supervisor.rb
class Supervisor < ApplicationRecord
has_many :hours, dependent: :destroy
validates :first_name, presence: true
validates :last_name, presence: true
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: {with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
validates :address, presence: true
validates :telephone, presence: true
validates :role, presence: true
validates :organization, presence: true
has_secure_password
validates :password, presence: true, length: { minimum: 6 }
end
和我的html实际格式
<%= form_for @hour, url: hours_path do |hour_builder| %>
<%= render 'shared/error_messages', object: hour_builder.object %>
<%= hour_builder.label :content, "Hours to input" %>
<%= hour_builder.text_field :content, class: 'form-control' %>
<%= hour_builder.simple_fields_for @supervisor do |supervisor_builder| %>
<%= render 'shared/error_messages', object: supervisor_builder.object %>
<%= supervisor_builder.label :first_name, "First name of supervisor" %>
<%= supervisor_builder.text_field :first_name, class: 'form-control' %>
<%= supervisor_builder.label :email, "Email of supervisor" %>
<%= supervisor_builder.text_field :email, class: 'form-control' %>
<%= supervisor_builder.label :telephone, "Telephone number of Supervisor" %>
<%= supervisor_builder.text_field :telephone, class: 'form-control' %>
<% end %>
<%= hour_builder.submit 'Log Hours', class: 'btn btn-success' %>
<% end %>
`
答案 0 :(得分:1)
我认为您正在传递嵌套在小时参数中的主管参数。在控制器中,您不允许在小时控制器中使用主管参数。
将小时数控制器更新为
def hour_params
params.require(:hour).permit(:content, :approved, supervisor_attributes: [:id, :first_name, :last_name, :email, :telephone])
end
这是基于您创建的表单。我认为您需要重新研究小时数与主管和用户之间的关联。
我的建议是更改小时模型以接受超级用户模型的nested_attributes,并更改参数以允许超级用户属性和小时参数一起使用。
答案 1 :(得分:0)
尝试合并参数
def company_params params [:company] .merge!(users_attributes:{'0':user_params.to_h}) params.require(:company).permit( :name,:contact_person_name,:email, :contact_person_number,:terms_and_conditions, users_attributes:%i [ id first_name last_name密码 密码确认电话号码 ] ) 结束
def user_params params [:user] .merge!(电话号码:params [:company] [:contact_person_number]) params.require(:user).permit( :first_name,:last_name,:password, :password_confirmation,:phone_number ) 结束
就我而言,公司有很多用户。 我希望为你工作。
答案 2 :(得分:0)
感谢所有答案,我用它们全部来帮助我,但这是我所做的全部工作:
首先:我将此行添加到我的hour.rb
hour.rb
attr_accessor :supervisor
第二:我为小时模型添加了迁移,以使用主管模型作为参考
$ rails g migration add_supervisor_to_hours supervisor:references
第三:我更改了hours_controller.rb中的hour_params
hours_controller.rb
def hour_params
params.require(:hour).permit(:content, :email,
supervisor: [:id,:first_name, :email, :telephone])
end