Validating fields always giving invalid

57 views Asked by At

I'm kind of new to ruby on rails. I have a User model with the field email, group and city. I'm giving two registration forms, one for mentor and one for mentee. When I add validation to any field , it's always returning invalid.

I'm using form_for for displaying form. Is this invalid error because I'm displaying two forms? And I'm not using attr_accessible in my model . I'm using strong parameters in my controller.

My form for Mentor and mentee looks like this

%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"

in the user controller I have this

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

and I'm adding validation in the user model

class User < ActiveRecord::Base

    validates :work_city, :presence => true  

end

Now, even though i enter something into work location and submit, it's giving 'Error registration'.

2

There are 2 answers

3
SteveTurczyn On

With HAML, your fields should be nested under the form_for...

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

As it is no fields are being returned so that's why your validation is not passing.

maxcal also makes the good point that you can't distinguish the two forms... best to have all fields under the single form.

3
max On

Yes, the errors is due to the fact that you have two different forms. Thats how HTML forms work.

<form action="/foo" id="A" method="post">
  <input name="user[email]" type="email" value="[email protected]" />
</form>

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

When the user clicks the submit button only the values in form B will be sent.

You need to nest all your inputs under = form_for(@user).

Added.

On a side note. Creating a separate database column on users for every possible strength is not a very viable approach. Instead what is commonly used is called associations. We would say that a User has many Strengths and Strength has many users.

We call this relationship 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

Some examples of how associations can be used:

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

The database layout

This is really big topic and I'm not going to go farther into it here. There are tons of guide, tutorials and screencasts on how associations work. The official guides are good starting point: