Вопрос

I would like to add a shop in the user registration with Devise.

Let me explain: on the registration page, the user can check a box "create my shop now." If he checks, a form is displayed ans he can fill it. Then he submits the form, and User + Shop creates. I'd like to know the best method in the controller "UsersController", as well as models and views.

Thank you for your help

UPDATE : Here is my code

app/views/devise/registrations/new.html.haml :

#sign_up
  %h2 Create your account

  = simple_form_for resource, as: resource_name, url: registration_path(resource_name) do |f|
    = f.error_notification

    = f.input :email, autofocus: true, input_html: { class: 'input-block-level' }
    = f.input :password, input_html: { class: 'input-block-level' }

    #check_box_fields
      = check_box_tag :create_shop_now, nil, nil, data: { toggle: 'collapse', target: '#shop_part' }
      = label_tag :create_shop_now, "I want a shop !" , class: 'checkbox inline'

    #shop_part.collapse
      = f.simple_fields_for :shops do |s|
        = s.input :name
        = s.input :email
        = s.input :description
        = s.input :siren

    #submit= f.button :submit, "Sign up", class: 'btn btn-primary'
    %p By clicking on 'Sign up', you confirm that you accept the Terms of Use

app/controllers/users/registrations_controller.rb :

class Users::RegistrationsController < Devise::RegistrationsController
  def create
    # raise params.inspect
    build_resource

    if resource.save
      if params[:user][:create_shop_now]
        resource.shops << Shop.create(params[:user][:shops_attributes])
      end

      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_navigational_format?
        sign_up(resource_name, resource)
        respond_with resource, location: after_sign_up_path_for(resource)
      else
        set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" 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
      respond_with resource
    end
  end
end

app/models/user.rb :

class User < ActiveRecord::Base
  rolify

  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :email, :password, :password_confirmation, :remember_me,
              :first_name, :last_name, :birthday, :gender_cd, :shops_attributes

  validates_presence_of :password
  validates :email, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, on: :create }
  validates_presence_of :email

  has_many :assignments, dependent: :destroy
  has_many :shops, through: :assignments

  accepts_nested_attributes_for :shops

  after_initialize do
    shops.new
  end
end

UPDATE 2 : Here is working code

app/views/devise/registrations/new.html.haml

#sign_up
  %h2 Create your account

  = simple_form_for resource, as: resource_name, url: registration_path(resource_name) do |f|
    = f.error_notification

    = f.input :email, autofocus: true, input_html: { class: 'input-block-level' }
    = f.input :password, input_html: { class: 'input-block-level' }

    #check_box_fields
      = check_box_tag :create_shop_now, nil, nil, data: { toggle: 'collapse', target: '#shop_part' }
      = label_tag :create_shop_now, "I want a shop !" , class: 'checkbox inline'

    #shop_part.collapse
      = f.simple_fields_for :shops do |s|
        = s.input :name
        = s.input :email
        = s.input :description
        = s.input :siren

    #submit= f.button :submit, "Sign up", class: 'btn btn-primary'
    %p By clicking on 'Sign up', you confirm that you accept the Terms of Use

app/controllers/registrations_controller.rb

class Users::RegistrationsController < Devise::RegistrationsController

  def new
    resource = build_resource({})
    resource.shops.build
    respond_with resource
  end

  def create
    build_resource

    if resource.save
      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_navigational_format?
        sign_up(resource_name, resource)
        respond_with resource, location: after_sign_up_path_for(resource)
      else
        set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" 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
      respond_with resource
    end
  end
end

app/models/user.rb

class User < ActiveRecord::Base
  rolify

  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :email, :password, :password_confirmation, :remember_me,
              :first_name, :last_name, :birthday, :gender_cd, :shops_attributes

  validates_presence_of :password
  validates :email, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, on: :create }
  validates_presence_of :email

  has_many :assignments, dependent: :destroy
  has_many :shops, through: :assignments

  accepts_nested_attributes_for :shops
end
Это было полезно?

Решение

You can do this with accepts_nested_attributes_for and nested models.

Simple checkout

UPDATE:

add the shop attributes to the form (with fields_for => accepts_nested_attributes_for), hide these form fields. create an attr_accessor in your user model, for example called :create_shop_now, add this to your form. if :create_shop_now is checked, show the shop form fields..an user can fill in the fields. user submits the form to the create action. The create action could look like following:

def create
  build_resource

  if !params[:user][:create_show_now]
    params[:user].delete(:shops_attributes)
  end

  if resource.save
    if resource.active_for_authentication?
      set_flash_message :notice, :signed_up if is_navigational_format?
      sign_up(resource_name, resource)
      respond_with resource, :location => after_sign_up_path_for(resource)
    else
      set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" 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
    respond_with resource
  end
end

UPDATE2

Remove your after_initialize hook in the model and update your registrations#new action to following:

def new
  resource = build_resource({})
  resource.shops.build
  respond_with resource
end
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top