2016-10-29 54 views
1

我非常沮丧,因为我一直在为此工作数天,并且还没有找到解决方案。显示模态导轨中的错误

我有一个Visit.rb模型,除了常见的验证之外,我通过两种方法验证了两次验证(访问的开始日期不能在结束之前,并且没有可能让两次访问具有相同的开始日期,结束日期和visitor_id)。

我需要显示由我设置的模式中的这些错误我给予注册访问,但直到现在我从来没有成功。 其实每出现此错误时间:引发ArgumentError在VisitsController#创建 - 在形式上第一个参数不能包含零或为空

这里我做了什么直到现在......

访问。 RB

class Visit < ActiveRecord::Base 

    belongs_to   :employee 
    belongs_to   :visitor 
    default_scope -> { order(:created_at) } 

    validates :from,   presence: true,  uniqueness: { scope: [:to, :visitor_id] } 
    validates :to,   presence: true 
    validates :visitor_id, presence: true 
    validates :employee_id, presence: true 
    validate :valid_date_range_required 
    validate :visit_uniqueness 


    def valid_date_range_required 
     if (from && to) && (to < from) 
      errors[:base] << "Visit's end date cannot be prior to the start one." 
     end 
    end 


    def visit_uniqueness 
     if Visit.find_by(from: :from, to: :to, visitor_id: :visitor_id) 
      errors[:base] << "A visit with the same start date, end date and visitor already exists." 
     end 
    end 

end 

visits_controller.rb

class VisitsController < ApplicationController 
    before_action :logged_in_employee, only: [:create, :destroy] 
    before_action :correct_employee, only: [:destroy, :update ] 

    def create 
     @visit = current_employee.visits.build(visit_params) 
     if @visit.save 
      flash[:success] = "Visit added" 
      redirect_to employee_path(session[:employee_id], :act => 'guestsVisits') 
     else 
      @visits = current_employee.visits.all 
      @employee = current_employee 
      render 'employees/guestsVisits' 
     end 
    end 



    private 

     def visit_params 
      params.require(:visit).permit(:from, :to, :visitor_id, :employee_id) 
     end 

     def correct_employee 
      @visit = current_employee.visits.find_by(id: params[:id]) 
      redirect_to root_url if @visit.nil? 
     end 
end 

guestsVisits.rb(我认为)

<div class="jumbotron3 text-center"> 
    <div class="row"> 
    <h1>Guests Visits</h1> 
    <hr> 
    <%=render :partial =>"layouts/sidebar"%> 
    <div class="panel3"> 
     <div class="panel-body"> 

                <!-- Modal for Visit --> 
       <% if logged_in? %> 
       <%=render :partial =>"shared/error_messages"%> 

       <a class="btn icon-btn btn-success pos" data-toggle="modal" data-target="#visitModal"> 
       <span class="glyphicon btn-glyphicon glyphicon-plus img-circle text-success"></span> 
       Add a visit 
       </a> 

       <div id="visitModal" class="modal fade" role="dialog"> 
        <div class="modal-dialog"> 

         <div class="modal-content"> 

          <div class="modal-header"> 
           <button type="button" class="close" data-dismiss="modal">&times;</button> 
           <h4 class="modal-title">Add Visit</h4> 
          </div> 

          <div class="modal-body"> 
           <%= form_for(@visit) do |f| %> 

            <%= f.label :start_date %> 
            <%= f.date_field :from, class: 'form-control',:value => (f.object.from.strftime('%m/%d/%Y') if f.object.from) %> 
            <%= errors_for @visit, :from %><%if @visit.errors.any?%><br><%end%> 

            <br> 
            <%= f.label :end_date %> 
            <%= f.date_field :to, class: 'form-control',:value => (f.object.to.strftime('%m/%d/%Y') if f.object.to) %> 
            <%= errors_for @visit, :to %><%if @visit.errors.any?%><br><%end%> 

            <br> 
            <%= f.label :Visitor %> 
            <%= f.collection_select :visitor_id, Visitor.all, :id, :full_name, { :class=> "form-control", :include_blank => ''}%> 
            <%= errors_for @visit, :visitor_id %><%if @visit.errors.any?%><br><%end%> 
            <br> 
            <%= f.submit "Add visit", class: "btn btn-primary btn-color" %> 
           <% end %> 
          </div> 

          <div class="modal-footer"> 
           <button type="button" class="btn btn-info" data-dismiss="modal">Close</button> 
          </div> 

         </div> 
        </div> 
       </div> 
       <% end %> 
      </div> 
     </div> 
    </div> 
</div> 

我试图用两个不同的版本error_messages.rb

error_messages.rb(1)

<% if @visit && @visit.errors.any? %> 
    <div id="error_explanation"> 
    <div class="alert alert-danger"> 
     <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a> 
     The form contains <%= pluralize(@visit.errors.count, "error") %>.Open the modal for more details 
    </div> 
    </div> 
<% end %> 

的error_messages.rb(2)

<% if @visit.errors.any? %> 
    <ul class="errors"> 
    <% @visit.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
    <% end %> 
    </ul> 
<% end %> 

我希望有人能帮助我,因为我不明白如何弄清楚。 非常感谢。

编辑:

class Employee < ActiveRecord::Base 

    before_save { self.email = email.downcase } 
    before_create :confirmation_token 

    VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 

    has_many :macaddresss, dependent:  :destroy 
    has_many :visits,   dependent:  :destroy 
    has_many :visitors,  dependent:  :destroy 

    #Validations 
    validates :name, presence: true , length: { maximum:20 , minimum: 2 } 
    validates :lastname, presence: true, length: { maximum:20 , minimum: 2 } 
    validates :gender, presence: true 
    validates :dateofbirth, presence: true 
    validates :birth_country, presence: true 
    validates :birth_place, presence: true, length: { maximum:60} 
    validates :contry_of_residence, presence: true 
    validates :city_of_residence, presence: true, length: { maximum:60 } 
    validates :address, presence: true, length: { maximum:60 , minimum: 7 } 
    validates :email, presence: true, length: { maximum:243 } ,format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false } 
    validates :password, length: { minimum: 6 } 
    validates :terms_of_service, acceptance: { message: 'accept terms and conditions' } 

    [...] 

    has_secure_password 

    def Employee.digest(string) 
    cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : 
                BCrypt::Engine.cost 
    BCrypt::Password.create(string, cost: cost) 
    end 
end 

session_helper.rb

def current_employee 
    @current_employee ||= Employee.find_by(id: session[:employee_id]) 
    end 

EDIT2:

employees_controller.rb

class EmployeesController < ApplicationController 

    def show 

     if logged_in? 
      @employee = Employee.find(params[:id]) 
      @indirizzimac = current_employee.indirizzimacs.new 
      @visitor = current_employee.visitors.new 
      @visit = current_employee.visits.new 
      @visits = current_employee.visits.all 
      if params[:act]=='myData' 
       render 'myData' 
      elsif params[:act]=='myNetwork' 
        render 'myData' 
      elsif params[:act]=='temporaryUsers' 
       render 'temporaryUsers' 
      elsif params[:act]=='guestsVisits' 
       render 'guestsVisits' 
      elsif params[:act]=='myAccount' 
       render 'myAccount' 
      else 
       render 'show' 
      end 
     else 
      render 'static_pages/errorPage' 
     end 
    end 
end 
+1

您是否尝试重新启动dev服务器。代码看起来不错。 – MikDiet

+0

您可以上传current_employee或其模型的实施 –

+0

@Emmanuel Mtali添加! – Benny

回答

1

从我可以看到,如果创建访问失败,你实例@visits@employee实例变量,然后渲染“员工/ guestsVisits”您使用@visit其观点,我认为它没有内部empleyees控制器存在 所以它添加广告

def guestsVisits 
    @visit = current_employee.visits.build 
end 
+0

我正在寻找一种方法将@visit从VisitsController#create传递给EmployeesController#guestVisits –

+0

首先,感谢您的帮助!我试过,但不幸的是它没有工作。我甚至添加了employees_controller的重要部分,这样你就可以更容易地理解我在做什么。 – Benny

+0

好的!如果你仍然卡住,你可以上传到GitHub并添加我为贡献者“Mtali”我到目前为止没有任何事情做,我将很高兴帮助 –