【问题标题】:Checkbox returns false when checked instead of true - Rails 4.2Checkbox 在检查时返回 false 而不是 true - Rails 4.2
【发布时间】:2016-09-07 14:08:37
【问题描述】:


因此,我正在尝试使用 Ruby on Rails 为一个学校项目构建一个电子商务网站,并为用户使用 Devise。 我还在自学这门语言,但遇到了一个问题。

我有这个由 devise 生成的视图:views/devise/registrations/new.html.erb

<%= bootstrap_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <div class="field">
    <%= f.text_field :prenom, autofocus: true %>
 </div>

<div class="field">
  <%= f.text_field :nom, autofocus: true %>
</div>

<div class="field">
  <%= f.text_field :adresse, autofocus: true %>
</div>  

<div class="field">
  <%= f.number_field :cp, autofocus: true %>
</div>

<div class="field">
  <%= f.email_field :email, autofocus: true, placeholder: "Ex: votre@mail.com" %>
</div>

<div class="field">
  <% if @validatable %>
    <%= f.password_field :password, autocomplete: "off", placeholder: "8 caractères minimum" %>
  <% end %>
</div>

<div class="field">
  <%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>

<div class="field">
  <%= f.check_box :admin %>
</div>

<div class="actions">
  <%= f.submit "Sign up" %>
</div>

我知道我不应该在表单中提供管理字段,但这仅用于测试目的。 因此,当我选中复选框并提交此表单时,它返回 false。 我不知道为什么。非常感谢任何帮助和解释。

如果需要,我的 user_controller

class UsersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :destroy]

  # Fais appel à la méthode permettant de restreindre l'accès uniquement aux admins
  # before_filter :authorize_admin, only: :index

  # GET /users
  # GET /users.json
  def index
    @users = User.all
  end

  # GET /users/1
  # GET /users/1.json
  def show
    @user = User.find(params[:id])
  end

  # GET /users/new
  def new
    @user = User.new
  end

 # GET /users/1/edit
 def edit
 end

 # POST /users
 # POST /users.json
 def create
   @user = User.new(user_params)

   respond_to do |format|
     if @user.save
       format.html { redirect_to @user, notice: 'User was successfully created.' }
       format.json { render :show, status: :created, location: @user }
    else
      format.html { render :new }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end

# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
  respond_to do |format|
    if @user.update(user_params)
      format.html { redirect_to @user, notice: 'User 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

# DELETE /users/1
# DELETE /users/1.json
def destroy
  @user.destroy
  respond_to do |format|
    format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
    format.json { head :no_content }
  end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_user
  @user = User.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def user_params
  params.fetch(:user, {})
end
end

【问题讨论】:

  • 您能否在选中时发布参数以及复选框元素。例如
  • 我不确定这是否是你所要求的(对不起,我是一个无聊的菜鸟:D)

标签: ruby validation ruby-on-rails-4 checkbox devise


【解决方案1】:

关键位就在最后:

# Never trust parameters from the scary internet, only allow the white list through.
def user_params
  params.fetch(:user, {})
end

白名单(第二个参数)为空,因此所有参数都被忽略。如果您查看日志文件,您会看到有关参数被忽略的警告(您可以将其配置为引发异常)。

目前尚不清楚正在使用的控制器是您的用户控制器还是设计的注册控制器 - 您已经显示了其中一个的视图,但显示了另一个的控制器代码。

如果您使用的是 devise 的注册控制器,那么 UsersController 可能不相关 - 您通常会使用 before 过滤器来运行类似的东西

devise_parameter_sanitizer.permit(:sign_up, keys: [:admin, ...])

更改设计的默认允许字段列表。

【讨论】:

  • 抱歉耽搁了各位,感谢您的回答!看起来我实际上拼错了参数中的每一个参数......
【解决方案2】:

我在您的代码中没有看到您使用:admin 复选框值的位置,但我猜您可能只是询问该参数是否为true,就像这样?

if params[:user][:admin] == true
  do this...
else
  do this...
end

问题在于,如果选中,则提交的复选框值默认为"1"(注意它的类型为string)。任何值的文本字符串都不等于truefalse 的布尔值。

您可以将代码更新为:

if params[:user][:admin] == "1"
  do this...
else
  do this...
end

Rails 总是以string 值接收提交的参数

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    • 2014-04-24
    相关资源
    最近更新 更多