我在创建和保存嵌套资源的实例时遇到了问题,但我一直无法弄清楚该如何解决。我有一个用户和一个Jobs模型(用户has_many个作业和Job About_to用户)。现有用户应该能够通过该帐户发布/创建作业。
在控制台@ user.jobs.build / @ user.jobs.create中执行良好。但是,在视图中,当我单击“发布职位”并尝试提交相应的表单时,会收到以下错误(取决于我所做的更改):
当我使用@user = User.find(params [:id])和@job = @ user.jobs.build(user_jobs_params)将表单提交给Jobs#new时,错误是:
ActionController::ParameterMissing (param is missing or the value is empty:
当我使用@user = User.find(params [:id])和@job = @ user.jobs.build(user_jobs_params)将表单提交给Jobs#create时,错误是:
ActiveRecord::RecordNotFound (Couldn't find User with 'id'=):
SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", nil], ["LIMIT", 1]]
当我使用@user = current_user和@job = @ user.jobs.build(user_jobs_params)将表单提交给jobs#create时,错误是:
ActionController::ParameterMissing (param is missing or the value is empty:
SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", nil], ["LIMIT", 1]]
当我使用@user = current_user和@job = @ user.jobs.build(user_jobs_params)将表单提交给jobs#create时,使用<%= form_with(model:@user,:url)设置表单=> {:controller =>“ job”,:action =>“ create”},:html => {:method =>:post})做| f | %>,错误是:
ActionController::ParameterMissing (param is missing or the value is empty:
SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", nil], ["LIMIT", 1]]
当我想弄明白为什么在服务器日志中将用户加载到网站的每个页面上时,为什么找不到user_id或找不到user_id时,我不知所措。对于使用current_user(在我的会话助手中定义,并且需要用户登录才能使用)的情况尤其感到困惑。我也尝试使用@user = User.new来执行此操作,并且遇到了一些其他错误,如果这样做是合适的话,我可以添加这些错误,但这似乎没有任何意义,因为我没有尝试创建一个具有作业的新用户发布,而是创建一个属于现有用户的作业。
更新: 作业主管
def new
@user = User.find(params[:user_id])
@job = @user.jobss.build
end
尝试推动创建了两种不同的方法,但都没有效果!
def create
@user = User.find(params[:user_id])
@job = @user.jobs.build(user_job_params)
@job.save
end
def create
@user = User.find(params[:user_id])
@job = @user.jobs.create(user_job_params)
end
private
def user_job_params
params.require(:job).permit(:job_name,
:job_description, :job_deadline, :organization,
:organization_liaison_name, :organization_liaison_email)
end
end
用户控制器顶部
class User < ApplicationRecord
has_one :partner_profile
has_one :student_profile
has_many :jobs, inverse_of: :user
accepts_nested_attributes_for :student_profile, :partner_profile, update_only: true
attr_accessor :remember_token, :activation_token, :reset_token
before_save :downcase_email
before_create :create_activation_digest, :build_profile
validates :first_name, presence: true, length: { maximum: 50 }
validates :last_name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
validates :account_type, presence: true
我在创建和保存嵌套资源的实例时遇到了问题,但我一直无法弄清楚该如何解决。我有一个用户和一个Jobs模型(用户has_many个作业和Job About_to用户)。现有用户应该能够通过该帐户发布/创建作业。
在控制台@ user.jobs.build / @ user.jobs.create中执行良好。但是,在视图中,当我单击“发布职位”并尝试提交相应的表单时,会收到以下错误(取决于我所做的更改):
当我使用@user = User.find(params [:id])和@job = @ user.jobs.build(user_jobs_params)将表单提交给Jobs#new时,错误是:
ActionController::ParameterMissing (param is missing or the value is empty:
当我使用@user = User.find(params [:id])和@job = @ user.jobs.build(user_jobs_params)将表单提交给Jobs#create时,错误是:
ActiveRecord::RecordNotFound (Couldn't find User with 'id'=):
SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", nil], ["LIMIT", 1]]
当我使用@user = current_user和@job = @ user.jobs.build(user_jobs_params)将表单提交给jobs#create时,错误是:
ActionController::ParameterMissing (param is missing or the value is empty:
SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", nil], ["LIMIT", 1]]
当我使用@user = current_user和@job = @ user.jobs.build(user_jobs_params)将表单提交给jobs#create时,使用<%= form_with(model:@user,:url)设置表单=> {:controller =>“ job”,:action =>“ create”},:html => {:method =>:post})做| f | %>,错误是:
ActionController::ParameterMissing (param is missing or the value is empty:
SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", nil], ["LIMIT", 1]]
当我想弄明白为什么在服务器日志中将用户加载到网站的每个页面上时,为什么找不到user_id或找不到user_id时,我不知所措。对于使用current_user(在我的会话助手中定义,并且需要用户登录才能使用)的情况尤其感到困惑。我也尝试使用@user = User.new来执行此操作,并且遇到了一些其他错误,如果这样做是合适的话,我可以添加这些错误,但这似乎没有任何意义,因为我没有尝试创建一个具有作业的新用户发布,而是创建一个属于现有用户的作业。
更新: 作业主管
def new
@user = User.find(params[:user_id])
@job = @user.jobss.build
end
def create
@user = User.find(params[:user_id])
@job = @user.jobs.build(user_job_params)
@job.save
end
def create
@user = User.find(params[:user_id])
@job = @user.jobs.create(user_job_params)
end
private
def user_job_params
params.require(:job).permit(:job_name,
:job_description, :job_deadline, :organization,
:organization_liaison_name, :organization_liaison_email)
end
end
用户控制器顶部
class User < ApplicationRecord
has_one :partner_profile
has_one :student_profile
has_many :jobs, inverse_of: :user
accepts_nested_attributes_for :student_profile, :partner_profile, update_only: true
attr_accessor :remember_token, :activation_token, :reset_token
before_save :downcase_email
before_create :create_activation_digest, :build_profile
validates :first_name, presence: true, length: { maximum: 50 }
validates :last_name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
validates :account_type, presence: true
作业创建错误
Started POST "/users/2/jobs" for XXXX at 2018-07-05 19:42:33 +0000
Cannot render console from XXXX! Allowed networks: XXXX
Processing by JobsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"I1/okrk5p125FKrFx2Nhu4L78UNJFQIZPgjWfnzVnCCh5vzr9AuNxFkl2jHD+qjshlh170OI1o0Av8yGlek4Aw==", "job"=>{"job_name"=>"ega", "job_description"=>"", job_deadline(2i)"=>"", "job_deadline(3i)"=>"", "job_deadline(1i)"=>"", "organization"=>"", "organization_liaison_name"=>"", "organization_liaison_email"=>"", "program_manager_first_name"=>"", "program_manager_last_name"=>"", "program_manager_email"=>"", "program_manager_phone_number"=>""}, "commit"=>"Submit A New Job!", "user_id"=>"2"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]]
(0.1ms) begin transaction
SQL (2.9ms) INSERT INTO "jobs" ("user_id", job_name", "status", "job_description", "organization", "organization_liaison_name", "organization_liaison_email", "program_manager_first_name", "program_manager_last_name", "program_manager_email", "program_manager_phone_number", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [["user_id", 2], ["job_name", "ega"], ["status", "Pending Approval"], ["job_description", ""], ["organization", ""], ["organization_liaison_name", ""], ["organization_liaison_email", ""], ["program_manager_first_name", ""], ["program_manager_last_name", ""], ["program_manager_email", ""], ["program_manager_phone_number", ""], ["created_at", "2018-07-05 19:42:33.406656"], ["updated_at", "2018-07-05 19:42:33.406656"]]
(5010.0ms) commit transaction
(2.3ms) rollback transaction
Completed 500 Internal Server Error in 5023ms (ActiveRecord: 5015.4ms)
ActiveRecord::StatementInvalid (SQLite3::BusyException: database is locked: commit transaction):
app/controllers/jobs_controller.rb:28:in `create'
答案 0 :(得分:1)
由于您位于JobsController中,而不是UsersController中,因此您需要使用param中的user_id(不是id)来获取用户的id。尝试使用...
@user = User.find(params[:user_id])
此外,您不应在新动作中传递参数,而应仅在create动作期间传递参数。
在新动作中
@job = @user.jobs.build
在创建操作中
@job = @user.jobs.build(user_jobs_params)