I have a controller that handles the new and edit actions for a form with a model that accepts nested attributes form another. The 'new' action works just fine. However, when I go to the edit form and submit it, it says:
Routing Error
No route matches [PATCH] "/admins/employees"
Also, when I am on the edit page it won't show all the current information there. Only the 'email' shows what is currently in the DB. Normally, the edit page shows what is currently in the DB related to those attributes, but this form is just blank, with the exception of the email. Unfortunately, googling this particular issue didn't come up with anything relevant or helpful. I think there was something close that was with Rails 3 or something but wasn't right for my problem. I am using Rails 5.1.
My code is as follows:
Employee Controller
class Admins::EmployeesController < UserActionsController
def index
@employees = User.where(:company => @current_company)
end
def edit
@employee = User.find(params[:id])
end
def update
@employee = User.find(params[:id])
@employee.assign_attributes(employee_params)
if @employee.save
flash[:notice] = "Employee was updated."
redirect_to root_path
else
flash.now[:alert] = "There was an error saving the information. Please try again."
render :edit
end
end
def show
@employee = User.find(params[:id])
end
def new
@employee = User.new
end
def create
@employee = User.new(employee_params)
@employee.company = @current_company
if @employee.save
redirect_to admins_employees_path
else
render :new
end
end
private
def employee_params
params.require(:user).permit(:email, :password, :profile_attributes => [:firstName, :lastName, :title, :fullTime, :startDate])
end
end
Edit.html.erb
<!--BODY-->
<%= render partial: 'form', locals: { employee: @employee, profile_attributes: :profile_attributes } %>
<!--BODY END-->
_form.html.erb
<%= form_for employee, url: admins_employees_path do |f| %>
<div class="col-4 mb-3">
<% if employee.errors.any? %>
<div class="alert alert-danger">
<h4><%= pluralize(employee.errors.count, "error") %>.</h4>
<ul>
<% employee.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="row">
<div class="col p-0 mr-3">
<div class="form-group">
<%= f.label :email %>
<%= f.text_field :email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :password %>
<%= f.text_field :password, class: 'form-control' %>
</div>
<%= f.fields_for profile_attributes do |user_f| %>
<div class="form-group">
<label>First Name</label>
<%= user_f.text_field :firstName, :placeholder => 'First Name', class: 'form-control' %>
</div>
<div class="form-group">
<label>Last Name</label>
<%= user_f.text_field :lastName, :placeholder => 'Last Name', class: 'form-control' %>
</div>
<div class="form-group">
Job <%= user_f.label :title %>
<%= user_f.text_field :lastName, :placeholder => 'Title', class: 'form-control' %>
</div>
<div class="form-group">
<label>Employment Start Date</label>
<%= user_f.text_field :startDate, :placeholder => 'Start Date', class: 'form-control' %>
</div>
<% end %>
</div>
<div class="col-12 p-0">
<%= f.submit "Submit", :class => 'btn btn-primary btn-block btn-lg' %>
</div>
</div>
</div>
<% end %>
Thanks!
(EDIT) Routes
Prefix Verb URI Pattern Controller#Action
employees_accounts GET /employees/accounts(.:format) employees/accounts#index
POST /employees/accounts(.:format) employees/accounts#create
new_employees_account GET /employees/accounts/new(.:format) employees/accounts#new
edit_employees_account GET /employees/accounts/:id/edit(.:format) employees/accounts#edit
employees_account GET /employees/accounts/:id(.:format) employees/accounts#show
PATCH /employees/accounts/:id(.:format) employees/accounts#update
PUT /employees/accounts/:id(.:format) employees/accounts#update
DELETE /employees/accounts/:id(.:format) employees/accounts#destroy
admins_accounts GET /admins/accounts(.:format) admins/accounts#index
POST /admins/accounts(.:format) admins/accounts#create
new_admins_account GET /admins/accounts/new(.:format) admins/accounts#new
edit_admins_account GET /admins/accounts/:id/edit(.:format) admins/accounts#edit
admins_account GET /admins/accounts/:id(.:format) admins/accounts#show
PATCH /admins/accounts/:id(.:format) admins/accounts#update
PUT /admins/accounts/:id(.:format) admins/accounts#update
DELETE /admins/accounts/:id(.:format) admins/accounts#destroy
admins_employees GET /admins/employees(.:format) admins/employees#index
POST /admins/employees(.:format) admins/employees#create
new_admins_employee GET /admins/employees/new(.:format) admins/employees#new
edit_admins_employee GET /admins/employees/:id/edit(.:format) admins/employees#edit
admins_employee GET /admins/employees/:id(.:format) admins/employees#show
PATCH /admins/employees/:id(.:format) admins/employees#update
PUT /admins/employees/:id(.:format) admins/employees#update
DELETE /admins/employees/:id(.:format) admins/employees#destroy
registrations GET /registrations(.:format) registrations#index
POST /registrations(.:format) registrations#create
new_registration GET /registrations/new(.:format) registrations#new
edit_registration GET /registrations/:id/edit(.:format) registrations#edit
registration GET /registrations/:id(.:format) registrations#show
PATCH /registrations/:id(.:format) registrations#update
PUT /registrations/:id(.:format) registrations#update
DELETE /registrations/:id(.:format) registrations#destroy
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
user_password PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
POST /users/password(.:format) devise/passwords#create
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
user_registration PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
POST /users(.:format) devise/registrations#create
root GET /