I'm trying to pass a project ID from its show page to an employee_projects form where I can display the projects name. The error I'm getting is
undefined local variable or method `project_id' for #<EmployeeProjectsController:0xb8bdd58>.
employee_project controller:
# 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 form
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 %>
Projects show
<%= button_to "Assign!", new_employee_project_path(project_id: @project.id) , class: "btn btn-primary", :method => :get %>
Does anyone have any ideas on how to fix this?
In your controller try changing puts params[project_id]
to puts params[:project_id]
.
params
is a Hash
so you must refer to its keys with symbols, that is :project_id
. project_id
is just a variable that has no value assigned to it, hence the error undefined local variable or method 'project_id' for #
.
UPDATE ( "Couldn't find Project with 'id'="
fix)
The button in projects' show
view is not sending the parameter project_id
even though you are passing it to the new_employee_project_path
helper (check your url string and you will see it ending at ?
with no parameters). To fix it, i recommend using a regular form
instead of button_to
helper.
So, instead of this line:
<%= button_to "Assign!", new_employee_project_path(project_id: @project.id) , class: "btn btn-primary", :method => :get %>
Try using this 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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.