我有一個場景,在管理員限制創建“新”邀請時,管理員無法輸入 2 個值。它們旨在在以后的狀態下設定在用戶“注冊商”上。創作作業正常。
有問題的 2 個值是 :responded 和 :attending (RA)。Responded 默認設定為 false,而參加未設定,因此默認為 nil。
然后我有一個邀請的編輯頁面,管理員可以在其中更新任何資訊。想法是,如果邀請出現無法撤消的錯誤,管理員可以設定或重置為默認值。
編輯頁面適用于所有值,但 RA 不會保存到資料庫。它不會引發任何例外,并且在邀請控制器的邏輯中繼續進行,就好像它被保存了一樣。
注意:我可以更新這些值并將其保存到沒有任何問題的軌道 c
我添加了一個 binding.break 來查看引數,這就是我假設問題所在:
>params[:invite]不包括 RA。但如果我這樣做,>params我可以看到它的“外面”ActionController::Parameters
(rdbg) params
#<ActionController::Parameters
{"_method"=>"patch",
"authenticity_token"=>"keSUCTMDKkOYCVzCRKwaFuQN6EL9of-0SlyEIKEn6OHr_8766q23xh0LhcpU1GzPoM1xdr8XmGfuRQAmMw4gzg",
"invite"=>#<ActionController::Parameters
{
"username"=>"Jim",
"titulation"=>"Mr Jim",
"guest_count"=>"1",
"escort_count"=>"1",
"password"=>""
}
permitted: false>,
"attending"=>"true",
"responded"=>"true",
"commit"=>"Update Invite",
"controller"=>"invites",
"action"=>"update",
"id"=>"8"
} permitted: false>
(rdbg) params[:invite] # ruby
#<ActionController::Parameters
{
"username"=>"Jim",
"titulation"=>"Mr Jim",
"guest_count"=>"1",
"escort_count"=>"1",
"password"=>""
}
permitted: false>
(rdbg)
這里的架構部分:
create_table "invites", force: :cascade do |t|
t.string "username"
t.string "titulation"
t.integer "guest_count"
t.integer "escort_count"
t.boolean "responded"
t.boolean "attending"
t.string "password"
t.string "password_digest"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
邀請.rb (模型)
class Invite < ApplicationRecord
validates :username, presence: true, uniqueness: { case_sensitive: false }, length: { minimum: 3, maximum: 25 }
has_secure_password
attribute :responded, default: false
end
邀請控制器.rb:
注意:因為“新”頁面不允許管理員設定 RA 值,所以我有用于創建的邀請引數和用于更新的邀請擴展引數...如果已登錄_管理員,則定義新的?@invite = Invite.new else flash[:notice] = "只有管理員可以訪問該頁面。" redirect_to root_path end end
def create
@invite = Invite.new(invite_params) # does not include responded & attending
if @invite.save
flash[:notice] = "Created new invite for #{@invite.titulation}"
redirect_to invite_path(@invite.id)
end
end
def show
@invite = Invite.find(params[:id])
end
def edit
@invite = Invite.find(params[:id])
if @invite.responded
flash.now[:alert] = "Warning, this invite has responded. Make sure you know what you're doing."
end
end
def update
@invite = Invite.find(params[:id])
if @invite.update(invite_extended_params) # includes responded & attending
binding.break
flash[:notice] = "Update was saved."
redirect_to invite_path(@invite.id)
else
flash.now[:alert] = "Something went wrong"
render 'edit'
end
end
private
def invite_params
params.require(:invite).permit(:username, :titulation, :guest_count, :escort_count, :password)
end
def invite_extended_params
params.require(:invite).permit(:username, :titulation, :guest_count, :escort_count, :password, :responded, :attending)
end
end
編輯頁面代碼:
<%= form_with(model: @invite, class: "shadow p-3 mb-3 bg-info rounded", local: true) do |form| %>
<div class="form-group row">
<%= form.label :username, class: "col-2 col-form-label text-light" %>
<div class="col-10">
<%= form.text_field :username, class: "form-control shadow rounded", placeholder: "Enter Unique username" %>
</div>
</div>
<div class="form-group row">
<%= form.label :titulation, class: "col-2 col-form-label text-light" %>
<div class="col-10">
<%= form.text_field :titulation, class: "form-control shadow rounded", placeholder: "Enter Titulation" %>
</div>
</div>
<div class="form-group row">
<%= form.label :guest_count, class: "col-2 col-form-label text-light" %>
<div class="col-10">
<%= form.number_field :guest_count, class: "form-control shadow rounded", placeholder: "How many Guests for this invite?" %>
</div>
</div>
<div class="form-group row">
<%= form.label :escort_count, class: "col-2 col-form-label text-light" %>
<div class="col-10">
<%= form.number_field :escort_count, class: "form-control shadow rounded", placeholder: "How many 1's for this invite?" %>
</div>
</div>
<div class="form-group row">
<%= form.label :password, class: "col-2 col-form-label text-light" %>
<div class="col-10">
<%= form.text_field :password, class: "form-control shadow rounded", placeholder: "Preset password" %>
</div>
</div>
<div class="form-group row">
<div class="col-2 col-form-label text-light"> Attendance:
<div class="col-10">
<%= radio_button_tag(:attending, true) %>
<%= label_tag(:attending, "Yes") %>
<%= radio_button_tag(:attending, false) %>
<%= label_tag(:attending, "No") %>
</div>
</div>
<div class="form-group row">
<div class="col-2 col-form-label text-light"> Responded:
<div class="col-10">
<%= radio_button_tag(:responded, true, @invite.responded) %>
<%= label_tag(:responded, "Yes") %>
<%= radio_button_tag(:responded, false, !@invite.responded) %>
<%= label_tag(:responded, "No") %>
</div>
</div>
<%= form.submit 'Update Invite' %>
<% end %>
uj5u.com熱心網友回復:
單選按鈕作為沒有表單背景關系的標簽添加到 html 中,因此它們不包含在表單的邀請引數中。
要修復更改它們以使用form.radio_button. 例如,改變...
<%= radio_button_tag(:attending, true) %>
到
<%= form.radio_button(:attending, true) %>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/453540.html
