我试图将项目ID从其展示页面传递到employee_projects表单,在那里我可以显示项目名称。我得到的错误是
undefined local variable or method `project_id' for #<EmployeeProjectsController:0xb8bdd58>.
employee_project控制器:
# GET /employee_projects/new
def new
puts params[project_id]
@project = Project.find(params[:project_id])
session[:project_id] = @project.id
@employee_project = EmployeeProject.new
end
employee_projects表单
Assign worker for this project <%= project.projectName%><br>
<div class="field">
<%= f.label :employee_id %><br>
<%= f.collection_select :employee_id, Employee.all, :id, :empLastName, :prompt => "Select worker" %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
项目展示
<%= button_to "Assign!", new_employee_project_path(project_id: @project.id) , class: "btn btn-primary", :method => :get %>
有没有人对如何解决这个问题有任何想法?
答案 0 :(得分:0)
在您的控制器中尝试将puts params[project_id]
更改为puts params[:project_id]
。
params
是Hash
,因此您必须使用符号引用其键,即:project_id
。 project_id
只是一个没有赋值的变量,因此错误为undefined local variable or method 'project_id' for #
。
更新("Couldn't find Project with 'id'="
修正)
项目“show
视图中的按钮未发送参数project_id
,即使您将其传递给new_employee_project_path
帮助程序(请检查 url 字符串你将看到它以?
结尾,没有参数)。要解决此问题,我建议您使用常规form
代替button_to
帮助。
所以,而不是这一行:
<%= button_to "Assign!", new_employee_project_path(project_id: @project.id) , class: "btn btn-primary", :method => :get %>
尝试使用此form
:
<form action="<%= new_employee_project_path %>" method="get">
<input type="hidden" name="project_id" value="<%= @project.id %>">
<input type="submit" value="Assign!" class="btn btn-primary">
</form>