0

我有一个名为 org and department 的模型。每当一个组织注册时,它必须通过输入部门名称作为逗号分隔值来创建其拥有的部门列表。部门列表也可以稍后编辑。这些是我的模型:

0rg

class Org < ActiveRecord::Base
  has_many :departments, dependent: :destroy
  attr_accessible :name, :website, :department_list
  validates :name, presence: true
  validates :website, presence: true

  def department_list
    departments.collect { |d| d.department_name }.join(', ')
  end

  def department_list=(text)
    if id && text
      departments.destroy_all
      text.split(',').each do |d|
        departments.create(department_name: d.strip.capitalize)
      end
   end
 end
end

部门

class Department < ActiveRecord::Base
  attr_accessible :department_name, :org_id
  belongs_to :org
end

我的观点

<%= f.text_area :department_list, :cols => "10", :rows => "10" %>

问题:

当组织通过输入部门名称作为逗号分隔值进行注册时,它不会存储在数据库中。但是当组织稍后通过编辑操作更新该字段时,仅存储部门名称并且可以随时进一步编辑。我希望在组织注册时存储部门名称。

请帮忙。

编辑:

我的注册控制器:

class Webs::RegistrationsController < Devise::RegistrationsController
  prepend_before_filter :require_no_authentication, :only => [ :new, :create, :cancel ]
  prepend_before_filter :authenticate_scope!, :only => [:edit, :update, :destroy]

  # GET /resource/sign_up

  def new
    build_resource({})
    respond_with self.resource
  end

  # POST /resource
  def create
    build_resource(sign_up_params)

    # customized code begin

    # create a new child instance depending on the given user type



    child_class = params[:web][:user_type].camelize.constantize
    resource.role = child_class.new(params[child_class.to_s.underscore.to_sym])

    # first check if child instance is valid
    # cause if so and the parent instance is valid as well
    # it's all being saved at once
    valid = resource.valid?
    valid = resource.role.valid? && valid

    # customized code end

    if valid && 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
     render :new
    end
 end

 # GET /resource/edit
 def edit
   render :edit
 end

 # PUT /resource
 # We need to use a copy of the resource because we don't want to change
 # the current user in place.
 def update

   self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key)
   prev_unconfirmed_email = resource.unconfirmed_email if resource.respond_to?(:unconfirmed_email)

   if resource.update_with_password(account_update_params)
      resource.role.update_attributes(params[:org])
      resource.role.update_attributes(params[:user])
      if is_navigational_format?
        flash_key = update_needs_confirmation?(resource, prev_unconfirmed_email) ?
          :update_needs_confirmation : :updated
        set_flash_message :notice, flash_key
      end
      sign_in resource_name, resource, :bypass => true
      respond_with resource, :location => after_update_path_for(resource)
   else
      clean_up_passwords resource
     render :edit
   end
 end
end
4

0 回答 0