2016-07-28 137 views
1

我有兩個模型駐留和用戶。它們都包含roll_number屬性,我現在已經在常駐模型中輸入了數據,當用戶註冊這是一個Devise資源時,它會檢查常駐模型中的居民是否存在相同的roll_number?然後用戶可以註冊!所以基本上我添加的屬性(roll_number)制定的用戶模型,然後我編輯在這裏註冊記憶控制器的創建方法是它的代碼:定製設計控制器不工作

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