gpt4 book ai didi

forms - 如果在 Devise 注册表单中出现注册错误,则重定向到相同的路由 rails 4

转载 作者:行者123 更新时间:2023-12-04 17:59:23 28 4
gpt4 key购买 nike

我已自定义设计注册表单以显示在我的自定义页面中。我在主页上显示了我的注册表。 http://awesomescreenshot.com/0815w1h9b0 .

我需要在表单本身的顶部显示错误。如果我在没有填写字段的情况下单击“提交”按钮,它将重定向到“/users”页面并出现错误。我需要在我有表单的同一页面中显示错误。

application_helper.rb

module ApplicationHelper
def resource_name
:user
end

def resource_class
User
end

def resource
@resource ||= User.new
end

def devise_mapping
@devise_mapping ||= Devise.mappings[:user]
end
end

application_controller.rb

class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :authenticate_user!
before_filter :configure_devise_params, if: :devise_controller?


def configure_devise_params
devise_parameter_sanitizer.for(:sign_up) do |u|
u.permit(:first_name, :last_name, :gender, :dob, :token, :contact_number, :profile_by, :email, :password, :password_confirmation ,:religion_id ,:caste_id ,:sub_caste_id)
end
devise_parameter_sanitizer.for(:account_update) do |u|
u.permit(:first_name, :last_name, :dob, :contact_number, :profile_by, :email, :current_password)
end
end
# REDIRECT USER TO OUR CUSTOM PATH AFTER LOGIN
def after_sign_in_path_for(resource)
root_path
end
def after_update_path_for(resource)
show_profile_path
end
# REDIRECT USER TO OUR CUSTOM PATH AFTER LOGOUT
def after_sign_out_path_for(resource)
root_path
end
end

routes.rb

Rails.application.routes.draw do 
devise_for :users
get '/home'=>'home#index', as: 'home_path'
end

new.html.erb 在设计 View 下

    <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>

<div class="field">
<%= f.hidden_field :token, value: rand(13 ** 13), autofocus: true %>
</div>

<div class="field">
<%= f.label :First_Name %>
<%= f.text_field :first_name, autofocus: true, placeholder: 'Enter First Name' %>
</div>

<div class="field">
<%= f.label :Last_Name %>
<%= f.text_field :last_name, autofocus: true, placeholder: 'Enter Last Name' %>
</div>

<div class="field">
<%= f.label :date_of_birth %>
<%= f.text_field :dob, class: 'datepicker', placeholder: 'Enter Date of Birth' %>
<script type="text/javascript">
$('.datepicker').datepicker()
</script>
</div>

<div class="field">
<%= f.label :gender %>
<%= f.radio_button :gender, '1', :checked => true %> <span class="radio-names">Male</span>
<%= f.radio_button :gender, '2' %><span class="radio-names">Female</span>
</div>

<div class="field">
<%= f.label :email %>
<%= f.email_field :email, autofocus: true, placeholder: 'Enter Email Address' %>
</div>

<div class="field">
<%= f.label :profile_password %>
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %>
<%= f.password_field :password, autocomplete: "off", placeholder: 'Enter Password' %>
</div>

<div class="field">
<%= f.label :confirm_password %>
<%= f.password_field :password_confirmation, autocomplete: "off", placeholder: 'Enter Confirm Password' %>
</div>

<div class="field">
<%= f.label :register_for %>
<%= f.select :profile_by, [['Myself', 1], ['Dad', 2], ['Mom', 3], ['Brother', 4], ['Sister', 5], ['Relative', 6], ['Son', 7], ['Daughter', 8]],{},{class: 'select_tag'} %>
</div>

<div class="field">
<%= f.label :contact_number %>
<%= f.text_field :contact_number, autofocus: true, placeholder: 'Enter Contact Number +91' %>
</div>


<div class="field">
<%= f.label :Religion %>
<%= f.select :religion_id, options_from_collection_for_select(Religion.all, :id, :religion),{},{class: 'select_tag'} %>
</div>

<div class="field">
<%= f.label :caste %>
<%= f.select :caste_id, options_from_collection_for_select(Caste.all, :id, :caste_name),{},{class: 'select_tag'} %>
</div>

<div class="field">
<%= f.label :subcaste %>
<%= f.select :sub_caste_id, options_from_collection_for_select(Subcaste.all, :id, :sub_caste_name),{},{class: 'select_tag'} %>
</div>

<br>
<br>

<div class="actions">
<%= f.submit "Register", class: 'register_button' %>
</div>
<% end %>

如果在注册过程中出现错误,请帮助我如何在同一页面上显示错误。

最佳答案

你不需要覆盖设计 Controller 试试这个:

在 lib 文件夹中创建类并覆盖其中的设计失败方法,如下所示:

class CustomFailure < Devise::FailureApp
def redirect_url
#new_user_registration_path or your_path
end

def respond
if http_auth?
http_auth
else
redirect
end
end
end

然后把这个 config/initializers/devise.rb

 config.warden do |manager|
manager.failure_app = CustomFailure
end

还有一件事你必须自动加载 lib 文件,例如:

config.autoload_paths << Rails.root.join('lib')

将这一行放在 config/application.rb

关于forms - 如果在 Devise 注册表单中出现注册错误,则重定向到相同的路由 rails 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37387443/

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