【发布时间】: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