title: Signing up wih devise
body: <p>I have a Community model which has_many :users, and Users model which belongs_to :community.</p>
  
  <p>I need when a user signs up can create a Community in the same process and be assigned to this Community when sign up. If no community is created the user should not be created.</p>
  
  <p>How should I do it? Perhaps creating a custom devise controller for user... Any idea would be appreciated.</p>
  
  <p>Thanks</p>
  
  <hr>
  
  <p>After changes of first response:</p>
  
  <p>app/controllers/devise/registrations_controller.rb</p>
  
  <pre><code> # GET /resource/sign_up
      def new
        logger.info "1"
        build_resource({})
        render_with_scope :new
      end
  
  # POST /resource
  def create
    logger.info "2"
    build_resource
  
    if Community.save_both_a_new_user_and_a_new_instance(resource)
      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_navigational_format?
        sign_in(resource_name, resource)
        respond_with resource, :location =&gt; redirect_location(resource_name, resource)
      else
        set_flash_message :notice, :inactive_signed_up, :reason =&gt; resource.inactive_message.to_s if is_navigational_format?
        expire_session_data_after_sign_in!
        respond_with resource, :location =&gt; after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords(resource)
      respond_with_navigational(resource) { render_with_scope :new }
    end
  end
  </code></pre>
  
  <p>app/views/devise/registrations/new.html.erb</p>
  
  <pre><code>&lt;h2&gt;Sign up&lt;/h2&gt;
  
  &lt;%= form_for(resource, :as =&gt; resource_name, :url =&gt; registration_path(resource_name)) do |f| %&gt;
    &lt;%= devise_error_messages! %&gt;
  
    &lt;div&gt;&lt;%= f.label :email %&gt;&lt;br /&gt;
    &lt;%= f.email_field :email %&gt;&lt;/div&gt;
  
    &lt;div&gt;&lt;%= f.label :password %&gt;&lt;br /&gt;
    &lt;%= f.password_field :password %&gt;&lt;/div&gt;
  
    &lt;div&gt;&lt;%= f.label :password_confirmation %&gt;&lt;br /&gt;
    &lt;%= f.password_field :password_confirmation %&gt;&lt;/div&gt;
  &lt;/br&gt;
  &lt;h4&gt;Introduce la contraseña de tu comunidad&lt;/h4&gt;
  &lt;div&gt;&lt;%= f.number_field :community_id %&gt;&lt;/div&gt;
  &lt;p&gt;o Crea una nueva comunidad&lt;/p&gt;
  
  &lt;/br&gt;
  &lt;div&gt;&lt;%= f.submit "Sign up" %&gt;&lt;/div&gt;
  &lt;% end %&gt;
  
  &lt;%= render "links" %&gt;
  </code></pre>
  
  <p>app/views/devise/registrations/edit.html.erb</p>
  
  <pre><code>&lt;h2&gt;Edit &lt;%= resource_name.to_s.humanize %&gt;&lt;/h2&gt;
  
  &lt;%= form_for(resource, :as =&gt; resource_name, :url =&gt; registration_path(resource_name), :html =&gt; { :method =&gt; :put }) do |f| %&gt;
    &lt;%= devise_error_messages! %&gt;
  
    &lt;div&gt;&lt;%= f.label :email %&gt;&lt;br /&gt;
    &lt;%= f.email_field :email %&gt;&lt;/div&gt;
  
    &lt;div&gt;&lt;%= f.label :password %&gt; &lt;i&gt;(leave blank if you don't want to change it)&lt;/i&gt;&lt;br /&gt;
    &lt;%= f.password_field :password, :autocomplete =&gt; "off" %&gt;&lt;/div&gt;
  
    &lt;div&gt;&lt;%= f.label :password_confirmation %&gt;&lt;br /&gt;
    &lt;%= f.password_field :password_confirmation %&gt;&lt;/div&gt;
  
    &lt;div&gt;&lt;%= f.label :current_password %&gt; &lt;i&gt;(we need your current password to confirm your changes)&lt;/i&gt;&lt;br /&gt;
    &lt;%= f.password_field :current_password %&gt;&lt;/div&gt;
  
    &lt;div&gt;&lt;%= f.submit "Update" %&gt;&lt;/div&gt;
  &lt;% end %&gt;
  
  &lt;h3&gt;Cancel my account&lt;/h3&gt;
  
  &lt;p&gt;Unhappy? &lt;%= link_to "Cancel my account", registration_path(resource_name), :confirm =&gt; "Are you sure?", :method =&gt; :delete %&gt;.&lt;/p&gt;
  
  &lt;%= link_to "Back", :back %&gt;
  </code></pre>
  
  <p>GEMFILE:</p>
  
  <pre><code>source 'https://rubygems.org'
  gem 'rails', '3.2.2'
  gem 'sqlite3'
  gem 'json'
  gem 'execjs'
  gem 'therubyracer'
  gem 'devise'
  gem "cancan"
  gem 'paperclip'
  gem "aws-s3"
  gem 'pg'
  gem 'twitter-bootstrap-rails'
  
  group :assets do
    gem 'sass-rails',   '~&gt; 3.2.3'
    gem 'coffee-rails', '~&gt; 3.2.1'
    gem 'uglifier', '&gt;= 1.0.3'
  end
  
  gem 'jquery-rails'
  </code></pre>