2017-03-02 2 views
0

이 오류가 발생합니다. 인증에 실패했습니다. 전자 메일은 비워 둘 수 없습니다. 가입을 시도하면 비밀번호를 비워 둘 수 없습니다.. 나는 가입을 위해 devise의 사용자 정의 등록 컨트롤러를 사용하고 있습니다. 나는 사용자 대신에 모델 직원을 사용하고있다.유효성 검사 실패 : 이메일을 비워 둘 수 없습니다. 비밀번호를 비워 둘 수 없습니다.

사용자 정의 registration_controller.rb

class RegistrationsController < Devise::RegistrationsController 

    def new 
    @employee = Employee.new 
    @organisation = Organisation.new 
    getLocation 
    end 

    def create 
    @organisation = Organisation.new 
    setOrganizationValues 
    @employee = Employee.new 
    setEmployeeValues 

    Employee.transaction do 
     @organisation.save! 

     @employee.org_id = @organisation.org_id 
     @employee.save! 
    end 

    respond_to do |f| 
     f.html {redirect_to employees_path} 
    end 
     # if @employee.save 
     # flash[:notice] = "Employee was successfully created" 
     # redirect_to employees_path 
     # else 
     # flash[:notice] = "Employee was not created" 
     # end 
    end 

    def getLocation 
    @locations = Location.select("location_id, location_name") 
           .where("status = 1") 
           .order("location_name") 
    end 

    def setCountryName 
    loc = Location.select("location_name") 
           .where("location_id = ?",params[:country]) 
    @organisation.country = loc.first.location_name 
    end 

    def setOrganizationValues 
    p "***********setOrganizationValues****************" 
    @organisation.org_name = params["org_name"] 
    @organisation.location_id = params["country"] 
    @organisation.city = params["city"] 
    @organisation.state = params["state"] 
    @organisation.email_id = params["email_id"] 
    @organisation.address = params["address"] 
    setCountryName 
    end 

    def setEmployeeValues 
    p "***********setEmployeeValues****************" 
    @employee.email = params["email"] 
    @employee.password = params["password"] 
    @employee.first_name = params["first_name"] 
    @employee.middle_name = params["middle_name"] 
    @employee.last_name = params["last_name"] 
    # @employee.state = params["state"] 
    # @employee.email_id = params["email_id"] 
    # @employee.address = params["address"] 
    end 

end 

routes.rb

Rails.application.routes.draw do 
    # devise_for :employees, :controllers => { :registrations => "registrations" }, :path => "", :path_names => {:sign_out => "signout", :sign_up => "signup"} 

    devise_for :employees, :controllers => {:registrations => "registrations"} 

    # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 
    root 'organisations#index' 
    resources :organisations 
    resources :roles 
    resources :clients 
    resources :project_tasks 
    resources :projects 
    resources :employees 
    get '/projects', to: 'projects#index' 
    get '/employees', to: 'employees#index' 
end 

보기 new.html.erb

<%= form_for(resource, as: resource_name, url: registration_path(resource_name), :html => {:id => "example-advanced-form"}) do |f| %> 
<br><br> 
    <h3>Account</h3> 
    <fieldset> 
     <legend>Account Information</legend> 

     <%= f.label :email %><br> 
     <%= f.text_field :email, :id => "email-2", :class => "required email" %> 
     <%= f.label :password %><br> 
     <%= f.password_field :password, :id => "password-2", :class => "required" %> 
     <%= f.label :password_confirmation %><br> 
     <%= f.password_field :password_confirmation, :name => "confirm", :id => "confirm-2", :class => "required" %> 
     <p>(*) Mandatory</p> 
    </fieldset> 

    <h3>Profile</h3> 
    <fieldset> 
     <legend>Profile Information</legend> 

     <div class="container-fluid"> 
      <div class="row"> 
      <div class="col-lg-6"> 

     <label>First Name</label><br> 
     <%= text_field_tag "first_name",@employee.first_name, 
        :autocomplete => :off,:id => "name-2",:maxlength => 13,:required => true%> 
     <label>Middle Name</label><br> 
     <%= text_field_tag "middle_name",@employee.middle_name, 
        :autocomplete => :off,:id => "surname-2",:maxlength => 13,:required => true%> 
     <label>Last Name</label><br> 
     <%= text_field_tag "last_name",@employee.last_name, 
        :autocomplete => :off,:id => "surname-2",:maxlength => 13,:required => true%> 

     <label>Designation</label><br> 
     <select class="required"> 
      <option value="Developer">Developer</option> 
      <option value="Tester">Tester</option> 
      <option value="Manager">Manager</option> 
      <option value="Other">Other</option> 
     </select> 
     <br> 
      <label>Organisation Name</label><br> 
      <%= text_field_tag "org_name",@organisation.org_name, 
             :autocomplete => :off,:id => "phone",:maxlength => 13,:required => true%> 
      <label>organisation Business Type</label><br> 
      <select class="required"> 
      <option value="Business">Business</option> 
      <option value="Finance">Finance</option> 
      <option value="IT">IT</option> 
      <option value="Other">Other</option> 
      </select><br> 
      </div> 
      <div class="col-lg-6"> 
      <label>Number Of Employees</label><br> 
      <select class="required"> 
      <option value="0-10">0-10</option> 
      <option value="10-50">10-50</option> 
      <option value="50-100">50-100</option> 
      <option value="100-500">100-500</option> 
      <option value="500-1000">500-1000</option> 
      <option value="1000 above">1000 above</option> 
      </select><br> 

      <label>Address</label><br> 
      <%= text_field_tag "address",@organisation.address, 
             :autocomplete => :off,:id => "address",:required => true%> 
      <label>City</label><br> 
      <%= text_field_tag "city",@organisation.city, 
             :autocomplete => :off,:id => "city",:required => true%> 
      <label>State</label><br> 
      <%= text_field_tag "state",@organisation.state, 
             :autocomplete => :off,:id => "state",:required => true%> 
      <label>Country</label><br> 
      <!-- <%= text_field_tag "country",@organisation.country, 
             :autocomplete => :off,:id => "country",:required => true%> --> 
      <%= select_tag :country, options_for_select(@locations.collect{ |l| [l.location_name, l.location_id]})%> 
      <label>Organisation Email</label><br> 
      <%= text_field_tag "email_id",@organisation.email_id, 
             :autocomplete => :off,:id => "email_id",:required => true%> 


     </div> 
     </div> 
    </div> 

    </fieldset> 

    <h3>Finish</h3> 
    <fieldset> 
     <legend>Confirmatio</legend> 

     <p>We have sent you a mail for Activation of your Account. Kindly Activate your through the mail. If you haven't received any mail, please click </p> 
     <%= link_to "here" %> 
    </fieldset> 

<% end %> 

<script> 
var form = $("#example-advanced-form").show(); 

form.steps({ 
    headerTag: "h3", 
    bodyTag: "fieldset", 
    transitionEffect: "slideLeft", 
    onStepChanging: function (event, currentIndex, newIndex) 
    { 
     // Allways allow previous action even if the current form is not valid! 
     if (currentIndex > newIndex) 
     { 
      return true; 
     } 
     // Forbid next action on "Warning" step if the user is to young 
     if (newIndex === 3 && Number($("#age-2").val()) < 18) 
     { 
      return false; 
     } 
     // Needed in some cases if the user went back (clean up) 
     if (currentIndex < newIndex) 
     { 
      // To remove error styles 
      form.find(".body:eq(" + newIndex + ") label.error").remove(); 
      form.find(".body:eq(" + newIndex + ") .error").removeClass("error"); 
     } 
     form.validate().settings.ignore = ":disabled,:hidden"; 
     return form.valid(); 
    }, 
    onStepChanged: function (event, currentIndex, priorIndex) 
    { 
     // Used to skip the "Warning" step if the user is old enough. 
     if (currentIndex === 2 && Number($("#age-2").val()) >= 18) 
     { 
      form.steps("next"); 
     } 
     // Used to skip the "Warning" step if the user is old enough and wants to the previous step. 
     if (currentIndex === 2 && priorIndex === 3) 
     { 
      form.steps("previous"); 
     } 
    }, 
    onFinishing: function (event, currentIndex) 
    { 
     form.validate().settings.ignore = ":disabled"; 
     return form.valid(); 
    }, 
    onFinished: function (event, currentIndex) 
    { 
     form.submit(); 
     alert("Submitted!"); 
    } 
}).validate({ 
    errorPlacement: function errorPlacement(error, element) { element.before(error); }, 
    rules: { 
     confirm: { 
      equalTo: "#password-2" 
     } 
    } 
}); 
</script> 

employee.rb

class Employee < ApplicationRecord 

    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 
    has_many :tasks 
    belongs_to :organisation 
    belongs_to :role 
    accepts_nested_attributes_for :organisation 
    validates :first_name, presence: true, length: { minimum: 5 } 
    validates :last_name, presence: true, length: { minimum: 5 } 
end 

나는 레일에 초보자입니다. 아무도 나를이 문제를 해결하는 데 도움이 될 수 있습니까?

+0

'Employee' 모델을 게시 할 수 있습니까? –

+0

@GavinMiller 직원 모델에 오류가 사라진 –

답변

1

직원 정보를 설정하는 코드에 문제가있는 것처럼 보입니다. 이러한 매개 변수는 employee 해시에 싸여 얻을하고자하는 형태의 인스턴스 f.를 사용하여 email, passwordpassword_confirmation 태그를 선언했기 때문에

def setEmployeeValues 
    p "***********setEmployeeValues****************" 
    @employee.email = params["email"]    # <= this should be params[:employee]["email"] 
    @employee.password = params["password"]  # <= this should be params[:employee]["password"] 
    @employee.first_name = params["first_name"] # <= these lines below are ok 
    @employee.middle_name = params["middle_name"] # since you haven't used f. for them 
    @employee.last_name = params["last_name"] 
end 

. 그것은 다음과 같이 표시됩니다

params = { 
    "employee" => { 
    "email" => <value>, 
    "password" => <value> 
    }, 
    "first_name" => <value>, 
    "middle_name" => <value>, 
    "last_name" => <value> 
} 

을 그래서 당신이 전무를 반환 것 setEmployeeValuesparams[:email]을 수행 할 때. 대신 params[:employee][:email]을 입력하고 password을 입력하십시오.

서버 로그를 보면 컨트롤러에서 액세스 할 수있는 것이 무엇인지 분명해야합니다.

+0

이 추가되었습니다. ** 조직이 존재해야합니다 ** 지금이 오류가 발생하고 있습니다 –

+0

무엇이 문제인지 확인할 수 있습니까? –

+0

@GaneshKumar 나는이 줄을보고있는 것 : '@employee.org_id = @ 그 organisation.org_id'를해야 가능성이 '@employee.organization_id = @ organization.id' 또는 더 나은 : '@employee.organization = @ organization' –

관련 문제