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'.
With HAML, your fields should be nested under the form_for...
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.