我对RoR很新,所以我想通过做一些项目来学习。我目前正在研究的是为客户提供健身教练的应用程序。当我尝试为客户端创建新的评估时,似乎client_id参数未被传递给assessment_controller。 这是一行:
<%= link_to 'New assessment', new_assessment_path(client_id: @client.id), class: "btn btn-xs btn-success" %>
错误:找不到'id'=
的客户端然而,在新的评估视图中,在调试区域中我看到了:
--- !ruby/hash-with-ivars:ActionController::Parameters
elements:
client_id: '1'
controller: assessments
action: new
ivars:
:@permitted: false
当我点击“创建”时,会发生错误
assessment_controller:
def create
@assessment = Assessment.new(assessment_params)
@assessment.client = Client.find(params[:client_id])
respond_to do |format|
if @assessment.save
format.html { redirect_to @assessment, notice: 'Assessment was successfully created.' }
format.json { render :show, status: :created, location: @assessment }
else
format.html { render :new }
format.json { render json: @assessment.errors, status: :unprocessable_entity }
end
end
端
答案 0 :(得分:0)
我很确定您将new_assessment_path(client_id: @client.id)
分配给新页面,并且在提交表单时,客户端ID不会传递给创建操作。有许多不同的解决方案。
<强> 1。溶液强>
<%= link_to 'New assessment', new_assessment_path(client_id: @client.id), class: "btn btn-xs btn-success" %>
#assestment _form.html.erb file
<%= f.hidden_field_tag 'client_id', @client_id %>
#assessments_controller:
def new
@assessment = Assessment.new
@client_id = params[:client_id]
end
def create
@assessment = Assessment.new(assessment_params)
respond_to do |format|
if @assessment.save
format.html { redirect_to @assessment, notice: 'Assessment was successfully created.' }
format.json { render :show, status: :created, location: @assessment }
else
format.html { render :new }
format.json { render json: @assessment.errors, status: :unprocessable_entity }
end
end
#add :client_id
def assessment_params
params.require(:registration).permit(..., :client_id)
end