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

发布于 2024-11-15 22:48:01 字数 273 浏览 1 评论 0原文

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

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

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

谢谢!

I'd like to show a sign_UP form in anywhere on my application. I just know how to do this with a sign_in form, but with the sign_up form, the same method dont work.

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

I am trying for many days on the forums to find a solution for this issue. I hope someone here can help me.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

似最初 2024-11-22 22:48:01

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

我已将注册表单放入我的 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(索引视图使用的布局)

<% 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 %>

我找到了此代码这里

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

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

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

干杯!

Here's how I managed to did it.

I've put a sign up form in my home#index

My files:

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

You need that part because Devise works with something called resource and it should be defined so you can call your registration#new anywhere.

Like that, you should be able to register. However, I needed to display errors on the same page. Here's what I added:

layout/home.html.erb (the layout used by index 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 %>

I found this code here

And here's something I created: I saved my resource object if invalid in a session so that the user hasn't to fill every field again. I guess a better solution exists but it works and it's enough for me ;)

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

I think I didn't forget any code. Feel free to use whatever you need.

Cheers !

Oo萌小芽oO 2024-11-22 22:48:01

从控制台 railsgenerate devise:install 。这将生成所有设备视图。
之后,在视图 render 'devise/users/new' 或类似的内容中,现在无法检查语法。

rails generate devise:install from console. This will generate all devise views.
After that in the view render 'devise/users/new' or something similar, cant check syntax now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文