自定义设计控制器无法正常工作

时间:2016-07-28 11:22:08

标签: ruby-on-rails ruby authentication devise

我有两个模型驻留和用户。它们都包含roll_number属性,我现在已经在驻留模型中输入了数据,当用户注册是Devise资源时,它检查驻留模型中是否存在相同的roll_number?然后用户可以注册!!所以基本上我将属性(roll_number)添加到Devise的用户模型中,然后我在这里编辑了注册控制器的Create方法的代码:

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 个答案:

答案 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

希望这会对你有所帮助