2016-07-28 7 views
1

У меня есть две модели резидент и пользователь. Оба они содержат атрибут roll_number, я уже ввел данные уже в резидентной модели, когда я хочу, когда пользовательский регистр, который является ресурсом Devise, проверяет, существует ли резидентный номер одного и того же roll_number в резидентной модели? И тогда пользователь может быть зарегистрирован! Поэтому в основном я добавил атрибут (roll_number) в модели пользователя DEViSE, а затем я редактировал метод создания контроллера Регистраций вот код этого:Пользовательский контроллер не работает

class Users::RegistrationsController < Devise::RegistrationsController 
before_action :configure_sign_up_params, only: [:create] 
before_action :configure_account_update_params, only: [:update] 

    # GET /resource/sign_up 
    # def new 
    # super 
    # end 

    def create 
    super 
    resident = Resident.find_by(roll_number: params[:roll_number]) 
    if resident.present? 
     @user = resident.create_user(params) 
     if @user.save 
     flash[:info] = "Welcome to messpay" 
     redirect_to root_url 
     else 
     render 'new' 
     end 
    else 
     flash[:danger] = "You have entered a worng Roll number or you are not a Resident" 
     redirect_to new_user_registration 
    end 

    end 

    # GET /resource/edit 
    # def edit 
    # super 
    # end 

    # PUT /resource 
    # def update 
    # super 
    # end 

    # DELETE /resource 
    # def destroy 
    # super 
    # end 

    # GET /resource/cancel 
    # Forces the session data which is usually expired after sign 
    # in to be expired now. This is useful if the user wants to 
    # cancel oauth signing in/up in the middle of the process, 
    # removing all OAuth session data. 
    # def cancel 
    # super 
    # end 

    # protected 


    def configure_sign_up_params 
    devise_parameter_sanitizer.permit(:sign_up, keys: [:roll_number,:resident_id]) 
    end 

    # If you have extra params to permit, append them to the sanitizer. 
    def configure_account_update_params 
    devise_parameter_sanitizer.permit(:account_update, keys: [:roll_number,:resident_id]) 
    end 

    # The path used after sign up. 
    # def after_sign_up_path_for(resource) 
    # super(resource) 
    # end 

    # The path used after sign up for inactive accounts. 
    # def after_inactive_sign_up_path_for(resource) 
    # super(resource) 
    # end 
end 

Но этот код не работает, и я получаю это когда я чувство формы: enter image description here

вот мой код формы:

<% provide(:title, 'Sign up for a free messpay account') %> 
<div class="row"> 
    <div class="col-xs-5 col-xs-offset-2" style="margin-top: 10%"> 
    <%= image_tag("signup.jpg", alt: "Thapar",width:"475",height: "331",class:"img-responsive") %> 

    </div > 
    <div class="col-xs-5" id="signup_form" style="margin-top: 10%"> 
    <%= image_tag("messpay.gif", alt: "Messpay",height: "38",width: "120") %> 

    <p style="font-size:30px;font-weight:100;"> Create an account </p> 

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> 
    <%= devise_error_messages! %> 
     <p style="font-size: 0.87em">Messpay account <%= link_to "What's this?","#" %></p> 
    <%= f.text_field :roll_number, class:'form-control',placeholder:"Roll number" %> 

    <%= f.email_field :email, class:'form-control',placeholder:"Email" %> 

    <%= f.password_field :password, class:'form-control',placeholder:"Password"%> 

    <%= f.password_field :password_confirmation, class: 'form-control',placeholder:"Password confirmation"%> 

    <%= f.submit "Create account", class: "btn btn-primary" %> 
    <% end %> 
    <p style="margin-top: 10%;color: gray;">Already have Messpay account<span onclick="openNav()"style="color:#0c90db;cursor:pointer;"> Login here !!</span></p> 
    </div> 
</div> 

Могу ли я с помощью Params Правильно? Я не могу понять, почему это происходит!

+1

Ваш PARAMS Паре вероятно области видимости. Попробуйте 'params [: user] [: roll_number]' –

+0

Где? при создании метода? '@user = resident.create_user (params)', эта строка? –

+0

Вам также нужно изменить файл маршрутов, чтобы фактически запустить свой код здесь. –

ответ

0

Я думаю, вы должны поставить ниже кода в appliction_controller.rb

before_action(:configure_permitted_parameters, if: :devise_controller?) 


def configure_permitted_parameters 
    devise_parameter_sanitizer.for(:sign_up) << [:roll_number,:resident_id]] 
    devise_parameter_sanitizer.for(:account_update) << [:roll_number,:resident_id]] 
end 

Надеется, что это поможет вам

+0

nope, проблема остается!, получая ошибку формы, что резидент не существует! –

+0

Я это сделал в регистрации контроллер !! –

+0

Можете ли вы поделиться с вами запросами журналов – SpunkyLive

Смежные вопросы