【问题标题】:Validating fields always giving invalid验证字段总是无效
【发布时间】:2015-06-22 19:57:21
【问题描述】:

我对 ruby​​ on rails 有点陌生。我有一个包含字段电子邮件、组和城市的用户模型。我给了两张登记表,一张给导师,一张给被指导者。当我向任何字段添加验证时,它总是返回无效。

我正在使用 form_for 来显示表单。这是因为我正在显示两个表单而导致的无效错误吗?而且我没有在我的模型中使用 attr_accessible 。我在控制器中使用了强参数。

我的导师和被指导者的表格如下所示

%h1 Mentor Registration
%h2 Partcipating PE/DE/Dir/Sr Dir/VP Information
.row
  .col-md-6.col-md-offset-3
    = form_for(@user) do |f|
     = f.label :'Cisco Email'
     = f.email_field :cisco_email
     = f.label :'Current group'
     = f.text_field :current_group
     = f.label :'Current work location,city'
     = f.text_field :work_city


%h2 Strengths: check at least one box that apply, can select at most 3
.row
  .col-md-6.col-md-offset-3
    = form_for(@user) do |f|
     = f.check_box :conflict_resolution
     = f.label :conflict_resolution, 'Conflict Resolution'
     = f.check_box :customer_know_how
     = f.label :customer_know_how, 'Customer Know How'
     = f.check_box :exec_acheive_results
     = f.label :exec_acheive_results, 'Executive to achieve results'
     = f.check_box :personal_branding
     = f.label :personal_branding, 'Personal Branding'
     = f.check_box :leading_change 
     = f.label :leading_change, 'Leading Change'
     = f.check_box :align_and_influence    
     = f.label :align_and_influence, 'Align and Influence'
     = f.check_box :managing_without_authority
     = f.label :managing_without_authority, 'Managing Without Authority'
     = f.check_box :win_win_negotiation
     = f.label :win_win_negotiation, 'Win-Win Negotiation'
     = f.check_box :career_exploration
     = f.label :career_exploration, 'Career Exploration'
     = f.check_box :effective_communication
     = f.label :effective_communication, 'Effective Communication'
     = f.check_box :think_out_box
     = f.label :think_out_box, 'Creative Thinking/Think Out Of the box'
     = f.check_box :tech_know
     = f.label :tech_know, 'Technical Know-How, List Areas'
     = f.text_field :tech_areas
     = f.check_box :other
     = f.label :other, 'Any Other'      
     = f.text_field :other_areas
     = f.submit "Register Me", class: "btn btn-primary"

在用户控制器中我有这个

class UsersController < ApplicationController


    def index
    end


    def show
       @user = User.find(params[:id])
    end

    def new
     @user = User.new
    end

    def create
        @user = User.new(user_params)    # Not the final implementation!
        if @user.save
            flash[:success] = "Welcome to the CSG Mentoring Tool!"
            redirect_to @user
        else
        flash[:notice] = "Error regsitering."
            render :new
             end
    end

    private
    ##Strong Parametres
    def user_params
     params.require(:user).permit(:user_role, :cisco_email, :current_group,     :work_city, :conflict_resolution, :customer_know_how, :personal_branding,
        :leading_change, :exec_acheive_results, :align_and_influence, :managing_without_authority, :career_exploration, :win_win_negotiation, :effective_communication, :think_out_box, :tech_know, :other, :tech_areas, :other_areas)          
    end
end

我正在用户模型中添加验证

class User < ActiveRecord::Base

    validates :work_city, :presence => true  

end

现在,即使我在工作位置输入了一些内容并提交,它仍然显示“注册错误”。

【问题讨论】:

  • My car is broken. how to fix it? - 如果没有看到您的代码,我们将无法以任何方式帮助您。
  • 我怀疑您收到的错误信息比这更具描述性。查看您的控制台或 development.log,看看到底发生了什么。
  • 错误注册是我在用户注册失败时在闪存中显示的内容。当我向任何一个字段添加验证时,它总是给出错误并且不允许用户注册。
  • 检查创建操作中的参数。在 else 块中使用 puts @user.errors.full_messages 来了解哪些验证失败。你可能会得到答案

标签: ruby-on-rails validation form-for


【解决方案1】:

使用 HAML,您的字段应该嵌套在 form_for... 下。

.row
  .col-md-6.col-md-offset-3
    = form_for(@user) do |f|
      = f.label :'Cisco Email'
      = f.email_field :cisco_email
      ...

由于没有返回任何字段,所以这就是您的验证未通过的原因。

maxcal 还提出了一个很好的观点,即您无法区分这两种形式...最好将所有字段都放在一个形式下。

【讨论】:

  • @maxcal 和@Steve:我将所有字段嵌套到一个表单中。它仍然给出相同的闪光信息。
  • 你是否缩进了这些字段?在我的插图中,我展示了 f.labelf.email_field 等与 form_for 相比需要缩进(我的意思是嵌套)
  • 我很抱歉!我是 Stackoverflow 的新手。不知道如何发布代码。当我将它缩进 4 个空格时,我错过了表单的缩进。
【解决方案2】:

是的,错误是由于您有两种不同的表格。这就是 HTML 表单的工作原理。

<form action="/foo" id="A" method="post">
  <input name="user[email]" type="email" value="test@example.com" />
</form>

<form  action="/foo" id="B" method="post">
  <input name="user[user_name]" type="string" value="John Doe" />
  <input type="submit" />
</form>

当用户点击提交按钮时,只会发送表单 B 中的值。

您需要将所有输入嵌套在= form_for(@user) 下。

已添加。

附带说明。在users 上为每种可能的强度创建一个单独的数据库列并不是一种非常可行的方法。相反,通常使用的称为关联。我们会说User 有很多StrengthsStrength 有很多用户。

我们称这种关系为has_and_belongs_to_many

class User < ActiveRecord::Base
  # ...
  has_and_belongs_to_many :strengths
  # ... more code
end

class Strength
  # description: string
  has_and_belongs_to_many :users
end

如何使用关联的一些示例:

Strength.all # all the strengths
User.last.strengths
douchebags = User.joins(:strengths)
                 .where(strengths: { description: 'Personal Branding' })

这是一个非常大的话题,我不打算在这里深入探讨。有大量关于关联如何运作的指南、教程和截屏视频。官方指南是很好的起点:

【讨论】:

  • 我将所有字段嵌套到一个表单中。它仍然给出相同的闪存错误消息
  • 一句友好的忠告;只是说它给了我一个错误实际上并不能让你在编程中取得任何进展。你首先需要培养自己的一些解决问题的能力。
  • 非常感谢您的意见。我很感激。我在控制器中使用了强参数,但现在我删除了它并在我的模型中添加了 attr_accessible。验证现在正在工作。我不知道这是否是正确的方法,但它正在工作。关于数据库,我有一组预定义的优势,我被要求在两个表单上显示。所以我只需要像这样设计我的表格。非常感谢:D
猜你喜欢
  • 2021-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多