我有一個名爲「Clients」的模型。客戶端模型屬於用戶模型(用戶模型與設計關聯)。還有另一種叫做「賣家」的模式,但他們沒有涉及這個問題。客戶可以手動付款給我(僅限現金)。當客戶支付這筆款項時,我可以讓他們訪問網站上的更多頁面。爲此,我向我的客戶添加了一個名爲「付款」的布爾變量,然後管理員(我)可以訪問其客戶資料,通過複選框將付費狀態從「未付」更新爲「付費」。只有管理員可以查看複選框。Check_box_tag在rails應用中不保留價值
這是形式的部分用於更新客戶端的信息:
<%= simple_form_for @client do |f| %>
<%= f.input :name, label: 'Full name', error: 'Full name is mandatory' %>
<%= f.input :company, label: 'Name of company' %>
<%= f.input :position, label: 'Position you hold at your company' %>
<%= f.input :number, label: 'Phone number' %>
<%= f.input :email, label: 'Enter email address' %>
<%= f.file_field :emp_img, label: 'Profile picture' %>
<%= check_box_tag 'paid', 'yes', false %>
<%= f.button :submit %>
<% end %>
然後我的客戶控制器:
class ClientController < ApplicationController
before_action :find_client, only: [:show, :edit, :update, :destroy]
def index
end
def show
end
def new
@client = current_user.build_client
end
def create
@client = current_user.build_client(client_params)
if @client.save
redirect_to clients_path
else
render 'new'
end
end
def edit
end
def update
end
def destroy
@client.destroy
redirect_to root_path
end
private
def client_params
if current_user.user_type == :admin
params[:client][:paid] = params[:client][:paid] == 'yes'
params.require(:client).permit(:paid, :name, :company, :position, :number, :email, :client_img)
else
params.require(:client).permit(:name, :company, :position, :number, :email, :client_img)
end
end
def find_client
@client = Client.find(params[:id])
end
end
當我去到客戶的個人資料,並點擊「更新」的信息,我得到部分表格,因爲複選框沒有被選中。我點擊它並更新配置文件,沒有錯誤,帶我回到配置文件。但是當我再次點擊更新時,複選框未被選中。它不保留複選框的值。其他一切都保留下來。就像名字,公司等一樣。當我進入rails c時,即使點擊它並更新它,付費變量仍然是錯誤的。有誰知道爲什麼它可能是這樣嗎? 請不要低估,我是新來的。請讓我知道我需要更改而不會降低投票率。
https://github.com/plataformatec/simple_form#available-input-types-and-默認換每列型。使用'<%= f.input:paid,如::布爾%>' – Ruslan
@Ruslan同樣的事情,它不會被保留。沒有錯誤或任何東西。但每次表單加載時,都沒有檢查。爲什麼不更新? :(是否在控制器不保存更新? – LizzyTheLearner