自定义设计控制器不工作 [英] Custom devise controller not working

查看:159
本文介绍了自定义设计控制器不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型驻留和用户。它们都包含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])
如果resident.present?
@user = resident.create_user(params)
如果@ user.save
flash [:info] =欢迎使用messpay
redirect_to root_url
else
render'new'
end
else
flash [:danger] =你输入了一个破旧的卷号或者你不是一个居民
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
#强制在
#in之后通常过期的会话数据到期。如果用户想要在进程中间
#cancel oauth签入/ up,
#删除所有OAuth会话数据,这是有用的。
#def cancel
#super
#end

#protected


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

#如果您有额外的参数允许,请将它们附加到消毒剂上。
def configure_account_update_params
devise_parameter_sanitizer.permit(:account_update,keys:[:roll_number,:resident_id])
end

#注册后使用的路径。
#def after_sign_up_path_for(资源)
#super(resource)
#end

#注册不活动帐户后使用的路径。
#def after_inactive_sign_up_path_for(resource)
#super(resource)
#end
end

但是这个代码不工作,当我感觉到这种形式时,我得到这个代码:



这是我的表单代码:

 <%提供(:标题,'注册一个免费的messpay账户')%> 
< div class =row>
< div class =col-xs-5 col-xs-offset-2style =margin-top:10%>
<%= image_tag(signup.jpg,alt:Thapar,width:475,height:331,class:img-responsive)%>

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

< p style =font-size:30px; font-weight:100;>创建一个帐户< / p>

<%= form_for(资源,as:resource_name,url:registration_path(resource_name))do | f | %GT;
<%= devise_error_messages! %GT;
< p style =font-size:0.87em> Messpay account<%= link_to这是什么?,#%>< / p>
<%= f.text_field:roll_number,class:'form-control',占位符:卷号%>

<%= f.email_field:电子邮件,类:'form-control',占位符:电子邮件%>

<%= f.password_field:password,class:'form-control',占位符:密码%>

<%= f.password_field:password_confirmation,class:'form-control',占位符:密码确认%>

<%= f.submit创建帐户,类:btn btn-primary%>
<%end%>
< p style =margin-top:10%; color:gray>已经有Messpay帐户< span onclick =openNav()style =color:#0c90db; cursor:pointer; >登入这里!!< / span>< / p>
< / div>
< / div>

我正在使用Params吗?我不明白为什么会发生这种情况!

解决方案

我认为你应该把以下代码放在 appliction_controller.rb

  before_action(:configure_permitted_pa​​rameters,if::devise_controller?)


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

希望这将有助于您


I have two models resident and user. Both of them contains roll_number attribute, I have entered data already in resident model now I want, when user register which is an Devise resource it checks if resident is there of same roll_number in resident model? And then user can be registered !! So Basically I added attribute (roll_number) to Devise's User model, then I edited the Create method of Registrations controller here's the code of it :

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

But this code isn't working and I am getting this when I am feeling the form:

here's my form code:

<% 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>

Am I using Params Properly ? I can't understand eaxctly why this is happening !

解决方案

I think you should put below code in 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

Hope this will help you

这篇关于自定义设计控制器不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆