x-editable ActionController::ParameterMissing(参数缺失或值为空:employee)
x-editable ActionController::ParameterMissing (param is missing or the value is empty: employee)
我正在尝试获取 x-editable gem,在此处找到:Bootstrap-x-editable-rails GitHub。我一直在寻找 1-2 天,但未能解决问题,所以我将其发布在这里。
我尝试将 x-editable 功能集成到我的数据中table,我已经做到了可以单击 table 元素和将弹出编辑框。但是,当我按 enter 键确认更改时,我在控制台中收到此错误:
Started PUT "/employees/3" for 66.186.164.130 at 2015-03-18 16:15:59 +0000
Processing by EmployeesController#update as JSON
Parameters: {"PUT"=>{"company_name"=>"New Compa"}, "id"=>"3"}
Employee Load (33.9ms) SELECT "employees".* FROM "employees" WHERE "employees"."id" = LIMIT 1 [["id", 3]]
Completed 400 Bad Request in 38ms
ActionController::ParameterMissing (param is missing or the value is empty: employee):
app/controllers/employees_controller.rb:79:in `employee_params'
app/controllers/employees_controller.rb:51:in `update'
Rendered /usr/local/rvm/gems/ruby-2.1.4@rails4/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/_source.erb (5.5ms)
Rendered /usr/local/rvm/gems/ruby-2.1.4@rails4/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/_trace.text.erb (1.1ms)
Rendered /usr/local/rvm/gems/ruby-2.1.4@rails4/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/_request_and_response.text.erb (0.9ms)
Rendered /usr/local/rvm/gems/ruby-2.1.4@rails4/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/diagnostics.text.erb (109.0ms)
视图如下:
<table class="table table-striped table-bordered table-hover" id="sample_1">
<thead>
<tr>
<th>Company name</th>
<th>Family</th>
<th>First name</th>
<th>Last name</th>
<th>Dob</th>
<th>Sub status</th>
<th>Gender</th>
<th>Uses tobacco</th>
<th>Tobacco cessation</th>
<th>Emp status</th>
<th>Coverage type</th>
<th>Currently enrolled</th>
<th>Current anthem</th>
<th>Current plan</th>
<th>Quote</th>
<th>Premium</th>
</tr>
</thead>
<tbody>
<% @employees.each do |employee| %>
<tr>
<td>
<a href="#" data-xeditable="true" data-pk="<%= employee.id %>" data-model="PUT" data-type="text" data-name = "company_name" data-url="/employees/<%= employee.id %>" data-title="Enter title">
<%= employee.company_name %>
</a>
</td>
<td><%= employee.family_id %></td>
<td><%= employee.first_name %></td>
<td><%= employee.last_name %></td>
<td><%= employee.dob %></td>
<td><%= employee.sub_status %></td>
<td><%= employee.gender %></td>
<td><%= employee.uses_tobacco %></td>
<td><%= employee.tobacco_cessation %></td>
<td><%= employee.emp_status %></td>
<td><%= employee.coverage_type %></td>
<td><%= employee.currently_enrolled %></td>
<td><%= employee.current_anthem %></td>
<td><%= employee.current_plan_id %></td>
<td><%= employee.quote_id %></td>
<td><%= employee.premium %></td>
</tr>
<% end %>
</tbody>
</table>
这是视图的控制器:
class EmployeesController < ApplicationController
before_action :set_employee, only: [:show, :edit, :update, :destroy]
skip_before_filter :verify_authenticity_token
def import
Employee.import(params[:file])
redirect_to employees_url, notice: "Census imported."
end
# GET /employees
# GET /employees.json
def index
@employees = Employee.all
end
# GET /employees/1
# GET /employees/1.json
def show
end
# GET /employees/new
def new
@employee = Employee.new
end
# GET /employees/1/edit
def edit
end
# POST /employees
# POST /employees.json
def create
@employee = Employee.new(employee_params)
respond_to do |format|
if @employee.save
format.html { redirect_to @employee, notice: 'Employee was successfully created.' }
format.json { render :show, status: :created, location: @employee }
else
format.html { render :new }
format.json { render json: @employee.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /employees/1
# PATCH/PUT /employees/1.json
def update
respond_to do |format|
if @employee.update(employee_params)
format.html { redirect_to @employee, notice: 'Employee was successfully updated.' }
format.json { render :show, status: :ok, location: @employee }
else
format.html { render :edit }
format.json { render json: @employee.errors, status: :unprocessable_entity }
end
end
end
# DELETE /employees/1
# DELETE /employees/1.json
def destroy
@employee.destroy
respond_to do |format|
format.html { redirect_to employees_url, notice: 'Employee was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_employee
end
# Never trust parameters from the scary internet, only allow the white list through.
def employee_params
params.require(:employee).permit(:company_name, :family_id, :first_name, :last_name, :dob, :sub_status, :gender, :uses_tobacco, :tobacco_cessation, :emp_status, :coverage_type, :currently_enrolled, :current_anthem, :current_plan_id, :quote_id, :premium)
end
end
我知道 skip_before_filter :verify_authenticity_token
代码片段使应用程序容易受到攻击,并且不会长期保留它,它只是我解决之前遇到的另一个问题的创可贴。
员工路线:
import_employees POST /employees/import(.:format) employees#import
employees GET /employees(.:format) employees#index
POST /employees(.:format) employees#create
new_employee GET /employees/new(.:format) employees#new
edit_employee GET /employees/:id/edit(.:format) employees#edit
employee GET /employees/:id(.:format) employees#show
PATCH /employees/:id(.:format) employees#update
PUT /employees/:id(.:format) employees#update
DELETE /employees/:id(.:format) employees#destroy
如果我可以提供任何其他帮助找到解决方案,请告诉我。
在您的控制器中,您的强参数定义如下所示:
def employee_params
params.require(:employee).permit(:company_name, :family_id, :first_name, :last_name, :dob, :sub_status, :gender, :uses_tobacco, :tobacco_cessation, :emp_status, :coverage_type, :currently_enrolled, :current_anthem, :current_plan_id, :quote_id, :premium)
end
这意味着它希望提交的数据包含一个名为 employee
的键,其值是包含任何这些允许的属性作为键的哈希值。
如果您查看正在提交的实际数据(在日志中),您会发现未遵循此格式:
{"PUT"=>{"company_name"=>"New Compa"}, "id"=>"3"}
我猜这是因为您在视图中的 <a>
元素上有 data-model="PUT"
。请尝试将其更改为 data-model="employee"
。
我正在尝试获取 x-editable gem,在此处找到:Bootstrap-x-editable-rails GitHub。我一直在寻找 1-2 天,但未能解决问题,所以我将其发布在这里。
我尝试将 x-editable 功能集成到我的数据中table,我已经做到了可以单击 table 元素和将弹出编辑框。但是,当我按 enter 键确认更改时,我在控制台中收到此错误:
Started PUT "/employees/3" for 66.186.164.130 at 2015-03-18 16:15:59 +0000
Processing by EmployeesController#update as JSON
Parameters: {"PUT"=>{"company_name"=>"New Compa"}, "id"=>"3"}
Employee Load (33.9ms) SELECT "employees".* FROM "employees" WHERE "employees"."id" = LIMIT 1 [["id", 3]]
Completed 400 Bad Request in 38ms
ActionController::ParameterMissing (param is missing or the value is empty: employee):
app/controllers/employees_controller.rb:79:in `employee_params'
app/controllers/employees_controller.rb:51:in `update'
Rendered /usr/local/rvm/gems/ruby-2.1.4@rails4/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/_source.erb (5.5ms)
Rendered /usr/local/rvm/gems/ruby-2.1.4@rails4/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/_trace.text.erb (1.1ms)
Rendered /usr/local/rvm/gems/ruby-2.1.4@rails4/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/_request_and_response.text.erb (0.9ms)
Rendered /usr/local/rvm/gems/ruby-2.1.4@rails4/gems/web-console-2.0.0/lib/action_dispatch/templates/rescues/diagnostics.text.erb (109.0ms)
视图如下:
<table class="table table-striped table-bordered table-hover" id="sample_1">
<thead>
<tr>
<th>Company name</th>
<th>Family</th>
<th>First name</th>
<th>Last name</th>
<th>Dob</th>
<th>Sub status</th>
<th>Gender</th>
<th>Uses tobacco</th>
<th>Tobacco cessation</th>
<th>Emp status</th>
<th>Coverage type</th>
<th>Currently enrolled</th>
<th>Current anthem</th>
<th>Current plan</th>
<th>Quote</th>
<th>Premium</th>
</tr>
</thead>
<tbody>
<% @employees.each do |employee| %>
<tr>
<td>
<a href="#" data-xeditable="true" data-pk="<%= employee.id %>" data-model="PUT" data-type="text" data-name = "company_name" data-url="/employees/<%= employee.id %>" data-title="Enter title">
<%= employee.company_name %>
</a>
</td>
<td><%= employee.family_id %></td>
<td><%= employee.first_name %></td>
<td><%= employee.last_name %></td>
<td><%= employee.dob %></td>
<td><%= employee.sub_status %></td>
<td><%= employee.gender %></td>
<td><%= employee.uses_tobacco %></td>
<td><%= employee.tobacco_cessation %></td>
<td><%= employee.emp_status %></td>
<td><%= employee.coverage_type %></td>
<td><%= employee.currently_enrolled %></td>
<td><%= employee.current_anthem %></td>
<td><%= employee.current_plan_id %></td>
<td><%= employee.quote_id %></td>
<td><%= employee.premium %></td>
</tr>
<% end %>
</tbody>
</table>
这是视图的控制器:
class EmployeesController < ApplicationController
before_action :set_employee, only: [:show, :edit, :update, :destroy]
skip_before_filter :verify_authenticity_token
def import
Employee.import(params[:file])
redirect_to employees_url, notice: "Census imported."
end
# GET /employees
# GET /employees.json
def index
@employees = Employee.all
end
# GET /employees/1
# GET /employees/1.json
def show
end
# GET /employees/new
def new
@employee = Employee.new
end
# GET /employees/1/edit
def edit
end
# POST /employees
# POST /employees.json
def create
@employee = Employee.new(employee_params)
respond_to do |format|
if @employee.save
format.html { redirect_to @employee, notice: 'Employee was successfully created.' }
format.json { render :show, status: :created, location: @employee }
else
format.html { render :new }
format.json { render json: @employee.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /employees/1
# PATCH/PUT /employees/1.json
def update
respond_to do |format|
if @employee.update(employee_params)
format.html { redirect_to @employee, notice: 'Employee was successfully updated.' }
format.json { render :show, status: :ok, location: @employee }
else
format.html { render :edit }
format.json { render json: @employee.errors, status: :unprocessable_entity }
end
end
end
# DELETE /employees/1
# DELETE /employees/1.json
def destroy
@employee.destroy
respond_to do |format|
format.html { redirect_to employees_url, notice: 'Employee was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_employee
end
# Never trust parameters from the scary internet, only allow the white list through.
def employee_params
params.require(:employee).permit(:company_name, :family_id, :first_name, :last_name, :dob, :sub_status, :gender, :uses_tobacco, :tobacco_cessation, :emp_status, :coverage_type, :currently_enrolled, :current_anthem, :current_plan_id, :quote_id, :premium)
end
end
我知道 skip_before_filter :verify_authenticity_token
代码片段使应用程序容易受到攻击,并且不会长期保留它,它只是我解决之前遇到的另一个问题的创可贴。
员工路线:
import_employees POST /employees/import(.:format) employees#import
employees GET /employees(.:format) employees#index
POST /employees(.:format) employees#create
new_employee GET /employees/new(.:format) employees#new
edit_employee GET /employees/:id/edit(.:format) employees#edit
employee GET /employees/:id(.:format) employees#show
PATCH /employees/:id(.:format) employees#update
PUT /employees/:id(.:format) employees#update
DELETE /employees/:id(.:format) employees#destroy
如果我可以提供任何其他帮助找到解决方案,请告诉我。
在您的控制器中,您的强参数定义如下所示:
def employee_params
params.require(:employee).permit(:company_name, :family_id, :first_name, :last_name, :dob, :sub_status, :gender, :uses_tobacco, :tobacco_cessation, :emp_status, :coverage_type, :currently_enrolled, :current_anthem, :current_plan_id, :quote_id, :premium)
end
这意味着它希望提交的数据包含一个名为 employee
的键,其值是包含任何这些允许的属性作为键的哈希值。
如果您查看正在提交的实际数据(在日志中),您会发现未遵循此格式:
{"PUT"=>{"company_name"=>"New Compa"}, "id"=>"3"}
我猜这是因为您在视图中的 <a>
元素上有 data-model="PUT"
。请尝试将其更改为 data-model="employee"
。