Note that there are some explanatory texts on larger screens.

plurals
  1. PODevise and shop creation
    primarykey
    data
    text
    <p>I would like to add a shop in the user registration with Devise.</p> <p>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.</p> <p>Thank you for your help</p> <p><strong>UPDATE :</strong> Here is my code</p> <p>app/views/devise/registrations/new.html.haml :</p> <pre><code>#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 </code></pre> <p>app/controllers/users/registrations_controller.rb :</p> <pre><code>class Users::RegistrationsController &lt; Devise::RegistrationsController def create # raise params.inspect build_resource if resource.save if params[:user][:create_shop_now] resource.shops &lt;&lt; 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 </code></pre> <p>app/models/user.rb :</p> <pre><code>class User &lt; 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 </code></pre> <p><strong>UPDATE 2 :</strong> Here is working code</p> <p>app/views/devise/registrations/new.html.haml</p> <pre><code>#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 </code></pre> <p>app/controllers/registrations_controller.rb</p> <pre><code>class Users::RegistrationsController &lt; 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 </code></pre> <p>app/models/user.rb</p> <pre><code>class User &lt; 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload