开发者

Display a custom sign_UP form anywhere in your app

开发者 https://www.devze.com 2023-03-13 23:00 出处:网络
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.

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!


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 !


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消