gpt4 book ai didi

ruby-on-rails - 在应用程序中的任意位置显示自定义 Sign_UP 表单

转载 作者:行者123 更新时间:2023-12-04 06:32:40 25 4
gpt4 key购买 nike

我想在我的申请中的任何位置显示 Sign_UP 表单。我只知道如何使用登录表单执行此操作,但使用登录表单时,相同的方法不起作用。

 [...]
<% unless user_signed_in? %>
<%= form_for("user", :url => user_session_path) do |f| %>
[...]

我在论坛上尝试了很多天来寻找此问题的解决方案。我希望这里有人可以帮助我。

谢谢!

最佳答案

这是我成功做到这一点的方法。

我已将注册表单放入我的 home#index

我的文件:

view/home/index.html.erb

<%= render :file => 'registrations/new' %>

helper/home_helper.rb

module HomeHelper
def resource_name
:user
end

def resource
@resource = session[:subscription] || User.new
end

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

def devise_error_messages!
return "" if resource.errors.empty?

messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
sentence = I18n.t("errors.messages.not_saved",
:count => resource.errors.count,
:resource => resource_name)

html = <<-HTML
<div id="error_explanation">
<h2>#{sentence}</h2>
<ul>#{messages}</ul>
</div>
HTML

html.html_safe
end

end

您需要该部分,因为 Devise 使用名为resource 的东西,并且应该对其进行定义,以便您可以在任何地方调用您的registration#new

这样,您应该就可以注册了。但是,我需要在同一页面上显示错误。这是我添加的内容:

layout/home.html.erb(索引 View 使用的布局)

<% flash.each do |name, msg| %>

# New code (allow for flash elements to be arrays)
<% if msg.class == Array %>
<% msg.each do |message| %>
<%= content_tag :div, message, :id => "flash_#{name}" %>
<% end %>
<% else %>

# old code
<%= content_tag :div, msg, :id => "flash_#{name}" %>

<% end %> #don't forget the extra end
<% end %>

我找到了这个代码here

这是我创建的内容:如果在 session 中无效,我会保存资源对象,以便用户不必再次填写每个字段。我想存在更好的解决方案,但它有效并且对我来说已经足够了;)

Controller /registration_controller.rb

def create
build_resource

if resource.save
if resource.active_for_authentication?
# We delete the session created by an incomplete subscription if it exists.
if !session[:subscription].nil?
session[:subscription] = nil
end

set_flash_message :notice, :signed_up if is_navigational_format?
sign_in(resource_name, resource)
respond_with resource, :location => redirect_location(resource_name, resource)
else
set_flash_message :notice, :inactive_signed_up, :reason => resource.inactive_message.to_s if is_navigational_format?
expire_session_data_after_sign_in!
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords(resource)
# Solution for displaying Devise errors on the homepage found on:
# https://stackoverflow.com/questions/4101641/rails-devise-handling-devise-error-messages
flash[:notice] = flash[:notice].to_a.concat resource.errors.full_messages
# We store the invalid object in session so the user hasn't to fill every fields again.
# The session is deleted if the subscription becomes valid.
session[:subscription] = resource
redirect_to root_path #Set the path you want here
end
end

我想我没有忘记任何代码。请随意使用您需要的任何内容。

干杯!

关于ruby-on-rails - 在应用程序中的任意位置显示自定义 Sign_UP 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6376718/

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