我想在 Devise 的用户注册中添加一个商店。
让我解释一下:在注册页面上,用户可以选中一个框“立即创建我的商店”。如果他检查,就会显示一个表格供他填写。然后他提交表单,User + Shop 创建。我想知道控制器“UsersController”中的最佳方法,以及模型和视图。
谢谢您的帮助
更新:这是我的代码
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
应用程序/控制器/用户/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
应用程序/模型/用户.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
更新2:这是工作代码
应用程序/视图/设计/注册/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
应用程序/控制器/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
应用程序/模型/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