2

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!

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Fernando Aureliano
  • 894
  • 12
  • 35

2 Answers2

6

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 !

Community
  • 1
  • 1
Lucas
  • 2,886
  • 1
  • 27
  • 40
  • Hi! Very very Thanks!! You help me a LOT!. The code works fine, but, whhen I registered an user, it recorded on db. But the terminal see the data correctly – Fernando Aureliano Jun 16 '11 at 23:24
  • here: Started POST "/users" for 127.0.0.1 at 2011-06-16 20:13:33 -0300 Processing by Devise::RegistrationsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"ULeV0DeOgB+Ss2qD43to/TifRfGlHxcGhJ6XDDNviBo=", "user"=>{"nome"=>"fsdasdf", "sobrenome"=>"fasdf", "email"=>"fernando@dz3.com.br", "tipo"=>"Distribuidores", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"} User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1 Redirected to http://localhost:3000/admin Completed 302 Found in 119ms – Fernando Aureliano Jun 16 '11 at 23:25
  • I cant understand why the user is not properly registered. =/ – Fernando Aureliano Jun 16 '11 at 23:27
  • Ha! I found a problem. The problem is because I try to register a user while I'm logged on But, how fix this? – Fernando Aureliano Jun 17 '11 at 00:15
  • You can't register if you're already logged in: that's a 'technical' choice from Devise team. You'll have to go deeper in `registrations_controller`. I've already seen that question on SO, so do a bit a search, you should be able to find your answer ;) – Lucas Jun 17 '11 at 06:45
  • 1
    Also, if the answer fit your question, you should accept it ;) – Lucas Jun 17 '11 at 09:39
  • great stuff, this was very helpfull :) – Francois Jul 25 '12 at 12:01
0

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.

Mikhail Nikalyukin
  • 11,867
  • 1
  • 46
  • 70