![](/img/trans.png)
[英]#<ActionController::ParameterMissing: param is missing or the value is empty: book>
[英]ParameterMissing param is missing or the value is empty
我有以下情況:我有一個用於admin的控制器,該控制器用於管理用戶(由Devise生成的模型)。
因此,我使用另一個控制器的動作來管理資源“用戶”。
這是我的文件:
admins_controller.rb
before_action :set_user, only: [:show, :edit, :update, :destroy]
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to admins_index_path, notice: 'Product was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
def set_user
@user = User.find(params[:id])
end
def user_params
params.require(:user).permit(:id)
end
edit.html.erb
<h1>Edit user</h1>
<%= form_for @user, :as => :patch, :url => admins_update_path(id: @user) do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this line_item from being saved:</h2>
<ul>
<% @user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :id %><br>
<%= f.text_field :id %>
</div>
<div class="field">
<%= f.label :email %><br>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :admin %><br>
<%= f.text_field :admin %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
問題是當我提交edit時,rails給我一個錯誤:
你能幫助我嗎? 我真的不知道如何解決謝謝
編輯:
更改視圖以刪除:as
選項,這將修復params需求錯誤:
= form_for @user, :url => admins_update_path(id: @user) do |f|
:as
告訴form_for
使用不同於您在params.require
調用中使用的基於默認類名的鍵名。
您還應該刪除f.text_field :id
您不希望有人對其進行編輯。
然后,您的allow permit(...)
塊將需要包含您要允許進行批量分配的形式的其他任何內容,最有可能的是您需要:allow permit(:email, :admin)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.