我想在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
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
更新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
答案 0 :(得分:0)
您可以使用accepts_nested_attributes_for和嵌套模型执行此操作。
简单结帐
<强>更新强>
将商店属性添加到表单(使用fields_for =&gt; accepts_nested_attributes_for),隐藏这些表单字段。在您的用户模型中创建一个attr_accessor,例如:create_shop_now,将其添加到您的表单中。 if:create_shop_now被选中,显示商店表单字段。用户可以填写字段。 user将表单提交给create动作。创建操作可能如下所示:
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 强>
删除模型中的after_initialize挂钩,并将注册#new action更新为以下内容:
def new
resource = build_resource({})
resource.shops.build
respond_with resource
end