验证字段始终无效

时间:2015-06-22 19:57:21

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

我对铁轨上的红宝石有点新意。我有一个用户模型,包括字段电子邮件,组和城市。我正在给两个注册表,一个是导师,一个是受指导者。当我向任何字段添加验证时,它总是返回无效。

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

Mentor和mentee的表格看起来像这样

%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

现在,即使我在工作地点输入内容并提交,也会给出“错误注册”。

2 个答案:

答案 0 :(得分:0)

使用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也说得好,你无法区分这两种形式......最好将所有字段都放在单一形式下。

答案 1 :(得分:0)

是的,错误是由于您有两种不同的形式。这就是HTML表单的工作方式。

<body>
    <div class="background">
        <div id="menu">
            <ul>
                <li><a href="#">Home</a></li>
                <li><a href="#">Investments</a></li>
                <li><a href="#">About</a></li>
                <li><a href="#">Contact</a></li>
                <li><a class="drop-menu" href="#" >Galleries</a>
                <ul id="dropdown">
                    <li><a href="#">Weddings</a></li>
                    <li><a href="#">Engagements</a></li>
                    <li><a href="#">Birthdays</a></li>
                    <li><a href="#">Family</a></li>
                </ul>
                </li>
            </ul>
        </div>
    </div>
<script type="text/javascript">
    var main = function() {
        $('#dropdown').hide();

        $('.drop-menu').mouseenter(function() {
            $('#dropdown').slideDown();
        });
        $('.drop-menu').mouseout(function() {
            $('#dropdown').slideUp();
        });
    };

    $(document).ready(main);
</script>
</body>

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

您需要将所有输入嵌套在<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> 下。

加。

旁注。在= form_for(@user)上为每种可能的强度创建单独的数据库列并不是一种非常可行的方法。相反,通常使用的是称为关联。我们会说users有很多UserStrengths有很多用户。

我们将这种关系称为Strength

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

The database layout

这是一个非常重要的主题,我不会在这里进一步深入讨论。有很多关于协会如何运作的指南,教程和截屏视频。官方指南是一个很好的起点: