2017-06-13 29 views
1

我有一個名爲「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時,即使點擊它並更新它,付費變量仍然是錯誤的。有誰知道爲什麼它可能是這樣嗎? 請不要低估,我是新來的。請讓我知道我需要更改而不會降低投票率。

+0

https://github.com/plataformatec/simple_form#available-input-types-and-默認換每列型。使用'<%= f.input:paid,如::布爾%>' – Ruslan

+0

@Ruslan同樣的事情,它不會被保留。沒有錯誤或任何東西。但每次表單加載時,都沒有檢查。爲什麼不更新? :(是否在控制器不保存更新? – LizzyTheLearner

回答

0

使用check_box形式助手代替成的形式,並且從client_params方法除去行:

params[:client][:paid] = params[:client][:paid] == 'yes' 
+0

我認爲這一行檢查,如果客戶已付款,如果是,然後允許(:支付)在客戶端params。我錯了嗎? – LizzyTheLearner

+0

你不需要定製在控制器端支付的價值。支付將根據check_box被自動選擇或不選擇。 –

+0

Thank you!It works! – LizzyTheLearner