我有两个模型驻留和用户。它们都包含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
这里是我的表单代码:
<% 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正常吗?我不明白为什么会发生这种情况!
你PARAMS削减可能作用域。试试'params [:user] [:roll_number]' –
哪里?在创建方法? '@user = resident.create_user(params)',这行? –
您需要更改路由文件以便在此处实际运行代码。 –