gpt4 book ai didi

ruby-on-rails - 自定义设计 Controller 不工作

转载 作者:行者123 更新时间:2023-12-05 07:48:26 24 4
gpt4 key购买 nike

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

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>

我是否正确使用了参数?我不明白为什么会这样!

最佳答案

我认为你应该把下面的代码放在 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

希望对你有帮助

关于ruby-on-rails - 自定义设计 Controller 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38634913/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com