0

我的問題是this question有點相關,以及相應的solution已經證明是有益的和有幫助的,但是有來自這個問題一點題外話,我想進一步解釋:Ruby on Rails的驗證不顯示錯誤消息

我已經使用active_record-acts_as寶石在devise寶石創建具有多個註冊的形式和單一的簽到表,這樣我有一個名爲客戶和其他客戶端類型,例如公司個人充當一個設計模型的模型客戶設計模型,以及所有這些MTI製作都基於o吶佈局從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_au thentication_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"]}> 

但是,如果我提交一個emp ty表單,並且驗證生效,但它不會顯示在基本錯誤所在的simple_form創建表單上。

爲了解決這個問題,我確實按照上面提到的解決方案,證明是成功的。但它會影響我的應用程序的其他佈局中的所有其他成功的驗證表單。只有使用client_authentication_layout.html.erb供電的表單纔不會在瀏覽器上顯示驗證。

我該如何解決這個問題?提前致謝。

回答

0

在您的形式嘗試:

<%= f.error_notification %> 

對於特定的列,你可以嘗試:

<%= f.error :name_of_your_column %>