我收到有关在我正在制作的网站上保存作业请求的错误。基本上,用户(候选人)将通过job / show.html.erb页面请求作业,然后评论将显示在show.html.erb页面上,其他候选人也已申请该作业。当用户在文本框中键入他们的名字并提交它时,我得到上面提到的错误。在线查看后,问题似乎在于我在模型中的实际情况。有什么想法吗?
RequestsController
class RequestsController < ApplicationController
before_action :authorise
#set_request, only: [:show, :edit, :update, :destroy]
def create
@job = Job.find params[:job_id]
@request = @job.requests.new(request_params) <- Error highlights this line
@request.candidate_id = @current_candidate.id #sets the user_id FK
@request.save #saves the @comment
# object to the comments table
respond_to do |format|
format.html{redirect_to @job}
end
end
private
def request_params
#This is the method ehich whitelists the data fields from the format
params.require(:request).permit(:content, :job_id, :candidate_id)
end
end
申请型号
class Request < ActiveRecord::Base
belongs_to :job, dependent: :destroy
has_many :candidates
end
候选模型
class Candidate < ActiveRecord::Base
has_secure_password
validates_uniqueness_of:can_email
belongs_to :request
validates :can_name, presence: true
validates :can_surname, presence: true
validates :college, presence: true
validates :can_email, presence: true
validates :address, presence: true
validates :experience, presence: true
validates :password_digest, presence: true
validates :college_year, numericality: { only_integer: true }
end
工作模式
class Job < ActiveRecord::Base
belongs_to :sector
has_many :requests, dependent: :destroy
validates :name, presence: true
validates :employer, presence: true
validates :sector, presence: true
validates :experience_req, presence: true
validates :job_info, presence: true
end
答案 0 :(得分:0)
假设您的routes.rb
文件中定义了以下路由:
resources :jobs do
resources :requests
end
嵌套路线,(见http://guides.rubyonrails.org/routing.html#nested-resources)
完全没必要通过job_id
部分传递requests
。即,您不需要将其作为输入包含在表单中,因为该URL已经通过了该参数。
查看你的服务器输出命中你的&#39; create`动作应该是这样的:
params = { job_id: 1, request: {content: "hello world", candidate_id: "123"}}
在这种情况下,您允许以下内容:
def request_params
params.require(:request).permit(:content, :candidate_id)
end
并且前两行的创建将是正确的:
@job = Job.find(params[:job_id])
@request = @job.requests.new(request_params)