0

у меня такая же проблема, как эта ссылка:Несколько моделей пользователей с Ruby On Rails и разработать, чтобы иметь отдельную регистрацию, но один общий логин

Multiple user models with Ruby On Rails and devise to have separate registration routes but one common login route

в моем приложении есть две модели

1) Компания

2) Сотрудник

моя модель пользователя

class User < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
    :recoverable, :rememberable, :trackable, :validatable 

    belongs_to :usr, :polymorphic => true 
end 

моя модель компании

class Company < ActiveRecord::Base 
    has_many :users, :as => :usr 
end 

моя модель Сотрудник

class Employee < ActiveRecord::Base 
    has_many :users, :as => :usr 
end 

приложение/просмотров/изобрести/регистрация/new.html.erb

<h2>Sign up</h2> 

<% 
    # customized code begin 

    params[:user][:user_type] ||= 'company' 

    if ["company", "employee"].include? params[:user][:user_type].downcase 
    child_class_name = params[:user][:user_type].downcase.camelize 
    user_type = params[:user][:user_type].downcase 
    else 
    child_class_name = "Company" 
    user_type = "company" 
    end 

    resource.usr = child_class_name.constantize.new if resource.usr.nil? 

    # customized code end 
%> 



<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> 
    <%= devise_error_messages! %> 

    <div class="field"> 
    <%= f.label :email %><br /> 
    <%= f.email_field :email, autofocus: true %> 
    </div> 

    <div class="field"> 
    <%= f.label :password %> 
    <% if @validatable %> 
    <em>(<%= @minimum_password_length %> characters minimum)</em> 
    <% end %><br /> 
    <%= f.password_field :password, autocomplete: "off" %> 
    </div> 

    <div class="field"> 
    <%= f.label :password_confirmation %><br /> 
    <%= f.password_field :password_confirmation, autocomplete: "off" %> 
    </div> 


    <% # customized code begin %> 
    <%= fields_for resource.usr do |rf| %> 
    <% render :partial => "#{child_class_name.underscore}_fields", :locals => { :f => rf } %> 
    <% end %> 

    <%= hidden_field :user, :user_type, :value => user_type %> 
    <% # customized code end %> 



    <div class="actions"> 
    <%= f.submit "Sign up" %> 
    </div> 
<% end %> 

<%= render "devise/shared/links" %> 

моя регистрация создать метод:

контроллеры/пользователей/registrations_controller.rb

class Users::RegistrationsController < Devise::RegistrationsController 
def create 
    build_resource 

    # customized code begin 

    # crate a new child instance depending on the given user type 
    child_class = params[:user][:user_type].camelize.constantize 
    resource.usr = child_class.new(params[child_class.to_s.underscore.to_sym]) 

    # first check if child instance is valid 
    # cause if so and the parent instance is valid as well 
    # it's all being saved at once 
    valid = resource.valid? 
    valid = resource.usr.valid? && valid 

    # customized code end 

    if valid && resource.save # customized code 
     if resource.active_for_authentication? 
     set_flash_message :notice, :signed_up if is_navigational_format? 
     sign_in(resource_name, resource) 
     respond_with resource, :location => redirect_location(resource_name, resource) 
     else 
     set_flash_message :notice, :inactive_signed_up, :reason => inactive_reason(resource) if is_navigational_format? 
     expire_session_data_after_sign_in! 
     respond_with resource, :location => after_inactive_sign_up_path_for(resource) 
     end 
    else 
     clean_up_passwords(resource) 
     respond_with_navigational(resource) { render :new } 
    end 
    end 
end 

мой помощник файл приложения:

module ApplicationHelper 
    def my_devise_error_messages! 
    return "" if resource.errors.empty? && resource.usr.errors.empty? 

    messages = usr_messages = "" 

    if !resource.errors.empty? 
     messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join 
    end 

    if !resource.usr.errors.empty? 
     usr_messages = resource.usr.errors.full_messages.map { |msg| content_tag(:li, msg) }.join 
    end 

    messages = messages + usr_messages 
    sentence = I18n.t("errors.messages.not_saved", 
         :count => resource.errors.count + resource.usr.errors.count, 
         :resource => resource.class.model_name.human.downcase) 

    html = <<-HTML 
    <div id="error_explanation"> 
    <h2>#{sentence}</h2> 
    <ul>#{messages}</ul> 
    </div> 
    HTML 

    html.html_safe 
    end 
end 

, но я получил ошибку что-то вроде этого, когда я открыть любой пользователь sign_up страницу:

localhost:3000/emloyees/sign_up **OR** 

localhost:3000/companies/sign_up 

ошибка в регистрации

ОШИБКА: -

enter image description here

так помогите мне, что я делаю неправильно .. ???

спасибо .. !!

ответ

0

Ваш вопрос очень широк, но для решения ошибки, которую вы отправили, это метод неопределенного ошибочного метода из-за вложенного хэша, которого не существует. Вот пример:

[1] pry(main)> params = {} 
=> {} 
[2] pry(main)> params[:user] 
=> nil 
[3] pry(main)> params[:user][:user_type] 
NoMethodError: undefined method `[]' for nil:NilClass 

Если ваш код ожидать, что Params [: пользователь] никогда не будет существовать без user_type, и вы хотите по умолчанию, что в компании, то это было бы просто:

[7] pry(main)> params[:user] ||= { user_type: "company" } 
=> {:user_type=>"company"} 
[8] pry(main)> params 
=> {:user=>{:user_type=>"company"}} 

Alternativley, если вы хотите просто вернуть строку, если params [: user] не существует, вы можете использовать || = или Hash#fetch со значением по умолчанию. Hash # fetch - более безопасный способ обработки ошибок imo.

[5] pry(main)> params.fetch(:user, "company") 
=> "company" 
+0

мой PARAMS [: пользователь] не дает "ноль", но дает ошибку "неопределенной локальной переменной или метод' параметрам для # " На самом деле я не получаю Params в регистрации новая страница. Я не знаю, почему. Я думаю, вам нужно ссылаться на ссылку, которую я упоминаю в начале вопроса, чтобы вы были более ясны. благодаря – Jiggs