Check_box_tag未在rails app

时间:2017-06-13 19:27:50

标签: ruby-on-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时,即使我点击并更新它,付费变量仍然是假的。有谁知道为什么会这样? 请不要downvote,我是新来的。请告诉我在没有低估的情况下需要改变的内容。

1 个答案:

答案 0 :(得分:0)

在表单中使用check_box表单助手代替check_box_tag,并从client_params方法中删除行:

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