我有一个控制器,用于处理表单的新操作和编辑操作,其中模型接受另一个嵌套属性。新的'行动很好。但是,当我转到编辑表单并提交时,它会说:
Routing Error
No route matches [PATCH] "/admins/employees"
此外,当我在编辑页面上时,它不会显示那里的所有当前信息。只有'电子邮件'显示数据库中当前的内容。通常,编辑页面会显示数据库中当前与这些属性相关的内容,但此表单只是空白,但电子邮件除外。不幸的是,谷歌搜索这个特定的问题并没有提出任何相关或有用的东西。我认为有一些与Rails 3或其他东西有关的东西,但对我的问题并不合适。我正在使用Rails 5.1。
我的代码如下:
员工控制器
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 %>
谢谢!
(编辑)路线
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 /
答案 0 :(得分:1)
您可以通过更改代码中的位来删除所有此错误。
edit.html.erb:
<%= form_for employee, url: admins_employee_path(params[:id]) do |f| %>
<%= render partial: 'form', locals: { employee: @employee, profile_attributes: :profile_attributes, f: f } %>
<% end %>
new.html.erb:
<%= form_for employee, url: admins_employees_path do |f| %>
<%= render partial: 'form', locals: { employee: @employee, profile_attributes: :profile_attributes, f: f } %>
<% end %>
并删除表单行并从form
partial
答案 1 :(得分:1)
为了您的信息我想添加rails是如此切割,它不需要告诉url with form_for,原因是如果form_form中的对象是新的,它将自动命中提交创建操作,如果对象是已经存在,但它会在提交时点击更新操作。那就是使用partial
的{{1}}和dry
轨道概念的美丽所以
您有一个部分form_for
用于编辑和新操作
这里只需更改一行: - 每次都不需要告诉url进行新的更新操作。
form
也没有必要传递<%= form_for employee do |f| %>
//your codes for form
<%end%>
局部变量,因为它将使用nested_attribites
对象自动提取
f
但是对于新操作,您需要<%= render partial: 'form', locals: { employee: @employee } %>
nested_attribute对象
build