【问题标题】:Devise and shop creation设计和商店创建
【发布时间】:2013-05-04 22:35:18
【问题描述】:

我想在 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:这是工作代码

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

【问题讨论】:

    标签: ruby-on-rails devise nested-resources


    【解决方案1】:

    您可以使用 Accepts_nested_attributes_for 和嵌套模型来做到这一点。

    简单结帐

    更新:

    将商店属性添加到表单(使用 fields_for => accept_nested_attributes_for),隐藏这些表单字段。在您的用户模型中创建一个 attr_accessor,例如名为 :create_shop_now,将其添加到您的表单中。如果 :create_shop_now 被选中,则显示商店表单字段。用户可以填写这些字段。用户将表单提交给创建操作。创建操作可能如下所示:

    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
    

    更新2

    删除模型中的 after_initialize 钩子并将您的 registrations#new 操作更新为以下操作:

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

    【讨论】:

    • 不是这样的。我正在搜索我必须在我的模型和控制器中插入的内容。例如,在用户控制器中。我应该做if resource.save if params[:user][:shops_attributes] resource.shops &lt;&lt; Shop.new(params[:user][:shops_attributes]) end end 我正在寻找最好的方法。清洁和工作。我正在做一个网站的 v2,所以我尽量做到最好
    • 谢谢。所以它没有更简单的方法。但是当我尝试这样做时,它会创建 2 个商店。一个有填充的字段,一个是空的。我试图删除我的after_initialize do shops.new end 但form_for :shops 不显示字段?
    • 我更新了我的答案。我认为 resource.save 已经创建了一个新商店,如果参数为空 => 将创建一个空商店。
    • 我试图删除它,它可以工作,但是当参数不为空时它会创建 2 个商店
    • 尝试移除您的 after_initialize 挂钩 - 设计方法“build_resource”也可能已经初始化了一个新商店。
    猜你喜欢
    • 2012-10-01
    • 2015-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-28
    • 2021-12-26
    • 2022-01-19
    相关资源
    最近更新 更多