我的问题与this question有点相关,相应的solution已被证明是有用且有用的,但是我想进一步解释这个问题的一些偏离:
我在active_record-acts_as gem上使用了devise gem来创建一个包含多个SignUp表单和一个SignIn表单的模型,这样我就有了一个名为 Client 的Devise模型,其他客户端类型(如公司和个人)充当客户端设计模型,所有这些MTI制作都基于与application.html.erb布局不同的布局,而我称之为client_authentication_layout.html.erb
总结一下我的问题,我的每个模型都有控制器以及相应的视图,我确实覆盖了客户端设计模型我可以将公司和个人表单/视图呈现为标签形式,这是成功的。
为清楚起见,以下是模型:
client.rb
class Client < ActiveRecord::Base
actable
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates :email, :encrypted_password, presence: true
end
company.rb
class Company < ActiveRecord::Base
acts_as :client
validates :company_name,
:company_url,
:country,
:contact_person,
:phone_number,
presence: true
end
individual.rb
class Individual < ActiveRecord::Base
acts_as :client
end
为清楚起见,这是公司控制人员:
class CompaniesController < ApplicationController
# GET /companies/new
def new
@company = Company.new
end
# POST /companies
# POST /companies.json
def create
@company = Company.new(company_params)
if @company.save
redirect_to root_path, notice: 'Company was successfully created.'
else
redirect_to new_client_registration_path
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_company
@company = Company.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def company_params
params.require(:company).permit(:company_logo, :company_name, :company_url, :country, :contact_person, :phone_number, :email, :password, :password_confirmation)
end
end
为清楚起见,我的观点如下:
公司/ new.html.erb
<%= simple_form_for(Company.new) do |f| %>
<div class="form-row">
<%= f.input :company_name, placeholder: 'Company Name', label: false %>
<%= f.input :company_url, placeholder: 'Company URL', label: false %>
<%= f.input :country, placeholder: 'Country', label: false %>
<%= f.input :contact_person, placeholder: 'Contact Person', label: false %>
<%= f.input :phone_number, placeholder: 'Phone Number', label: false %>
<%= f.input :email, required: true, autofocus: true, placeholder: 'Email', label: false %>
<%= f.input :password, required: true, placeholder: 'Password', label: false %>
<%= f.input :password_confirmation, required: true, placeholder: 'Password Confirmation', label: false %>
</div>
<%= render 'shared/two_breaks' %>
<div class='form-row'>
<div class="col-md-12 form-group">
<%= f.submit 'Get Started', class: 'btn btn-company' %>
</div>
<%= render 'shared/four_breaks' %>
</div>
<% end %>
个体/ new.html.erb
<%= simple_form_for(Individual.new) do |f| %>
<div class="form-inputs">
<%= f.input :first_name, placeholder: 'First Name', label: false %>
<%= f.input :last_name, placeholder: 'Last Name', label: false %>
<%= f.input :about_me, placeholder: 'Type in a little details about yourself.', label: false %>
<%= f.input :gender, placeholder: 'Choose gender', label: false %>
<%= f.input :country, placeholder: 'Country', label: false %>
<%= f.input :phone_number, placeholder: 'Phone Number', label: false %>
<%= f.input :email, required: true, autofocus: true, placeholder: 'Email', label: false %>
<%= f.input :password, required: true, placeholder: 'Password', label: false %>
<%= f.input :password_confirmation, required: true, placeholder: 'Password Confirmation', label: false %>
</div>
<%= render 'shared/two_breaks' %>
<div class='form-row'>
<div class="col-md-12 form-group">
<%= f.submit 'Get Started', class: 'btn btn-individual' %>
</div>
<%= render 'shared/four_breaks' %>
</div>
<% end %>
client_authentication_layout.html.erb
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="ROBOTS" content="NOODP" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>FarFlung Jobs</title>
<!-- /.You can include style.css into stylesheet_link_tag too. If you do so, dont forget to add style.css in asset.rb -->
<%= stylesheet_link_tag 'clients_authentication', media: 'all' %>
<%= javascript_include_tag 'clients_authentication' %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
我的标签式表格呈现公司和客户设计视图上的个人观点
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<!--col-sm-6 col-sm-offset-3 col-xs-4 col-xs-offset-3-->
<div class="panel panel-login">
<div class="panel-heading">
<div class="row">
<div class="col-xs-6 tabs">
<a href="#" class="active" id="company-form-link"><div class="company">COMPANY</div></a>
</div>
<div class="col-xs-6 tabs">
<a href="#" id="individual-form-link"><div class="individual">INDIVIDUAL</div></a>
</div>
</div>
</div>
<div class="panel-body">
<div class="row">
<div class="col-lg-12">
<div id="company-form" role="form" style="display: block;">
<h2>COMPANY</h2>
<%= render 'companies/form' %>
</div>
<div id="individual-form" role="form" style="display: none;">
<h2>INDIVIDUAL</h2>
<%= render 'individuals/form' %>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-12 text-center">
<span class="agreement">
<p>By clicking Get Started, you agree to the FarFlung <a tabindex="2" href="https://www.linkedin.com/legal/user-agreement">User Agreement</a>, </p>
<p><a tabindex="2" href="https://www.linkedin.com/legal/privacy-policy">Privacy Policy</a>, and <a tabindex="2" href="https://www.linkedin.com/legal/cookie-policy">Cookie Policy</a>.</p>
</span>
</div>
</div>
</div>
Rails控制台
>> c = Company.new
#<Company id: nil, company_logo: nil, company_name: nil, company_url: nil, country: nil, contact_person: nil, phone_number: nil, created_at: nil, updated_at: nil>
>> c.valid?
false
>> c.errors.full_messages
["Email can't be blank", "Email can't be blank", "Password can't be blank", "Encrypted password can't be blank", "Company name can't be blank", "Company url can't be blank", "Country can't be blank", "Contact person can't be blank", "Phone number can't be blank"]
>> c.errors
#<ActiveModel::Errors:0x74f6ee0 @base=#<Company id: nil, company_logo: nil, company_name: nil, company_url: nil, country: nil, contact_person: nil, phone_number: nil, created_at: nil, updated_at: nil>, @messages={:email=>["can't be blank", "can't be blank"], :password=>["can't be blank"], :encrypted_password=>["can't be blank"], :company_name=>["can't be blank"], :company_url=>["can't be blank"], :country=>["can't be blank"], :contact_person=>["can't be blank"], :phone_number=>["can't be blank"]}>
但是,如果我提交一个空表单,并且验证生效,那么它就不会在基础错误所在的simple_form创建表单上显示。
为了解决这个问题,我确实按照上面提到的上述解决方案,证明是成功的。但它会影响我的应用程序的其他布局中的所有其他成功验证表单。只有使用client_authentication_layout.html.erb
支持的表单才会在浏览器上显示验证。
我该如何解决这个问题?提前谢谢。
答案 0 :(得分:0)
在表单中尝试:
<%= f.error_notification %>
对于特定列,您可以尝试:
<%= f.error :name_of_your_column %>