简体   繁体   English

从文本字段获取值并将其存储为数组

[英]Get value from text field and store it as array

I have a text field in my database called departments where i want to store the list of departments. 我的数据库中有一个名为Departments的文本字段,我想在其中存储部门列表。 The user will enter the name of departments with comma separation. 用户将输入逗号分隔的部门名称。 For example: 例如:

department1, deaprtment2, department3

I want this value to be stored as array when the user submits the form. 我希望在用户提交表单时将此值存储为数组。 Also, i want the list of departments to show as a drop-down. 另外,我希望部门列表显示为下拉列表。 Finally, while updating the table , the department field should also be editable as before(update by entering texts separated by commas). 最后,在更新表时,部门字段也应该像以前一样可编辑(通过输入用逗号分隔的文本进行更新)。

EDIT: I have added this to my model: 编辑:我已经将此添加到我的模型:

class Org < ActiveRecord::Base
   serialize :department, Array
   attr_accessible :name, :department
   before_validation :update_department
   validates :name, presence: true
   def update_department
     if department_changed? and department.is_a?(String)
        self.department = self.department.split(',').collect(&:strip) 
    end
  end
end

and the view: 和视图:

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

now Whenever i try to sign up, the department field already has [] present and when i try to update the department is already ["[department1", "department2]"]. 现在,每当我尝试注册时,部门字段中就已经有[],而当我尝试更新部门时,部门中就已经[[[department1],“ department2]”]。

I want [] to be removed while signing up and only department1, department2 to show up when updating. 我希望在注册时删除[],并且在更新时仅显示department1,department2。

Please Help. 请帮忙。

The best way to do this would be via your models. 最好的方法是通过模型。 I am assuming that you have a model called Org and another called Department and that you have defined a has many relationship between the two. 我假设您有一个名为Org的模型和另一个称为Department的模型,并且您已经定义了两者之间有很多关系。 All you then need to do is in your Org model add the following code: 然后,您需要做的就是在您的组织模型中添加以下代码:

  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

Then in your view add a text box using @org.department_list. 然后在您的视图中使用@ org.department_list添加一个文本框。

EDIT: 编辑:

Based on your expanded question, you have department field in an org model that you want to store and show as an array and but edit as a simple text field. 根据您提出的问题,您在组织模型中有一个Department字段,您希望将其存储并显示为数组,但可以将其编辑为简单的文本字段。 My thoughts on this was that I don't like the idea of storing department data a field in org, it is a one to many relationship so department should be a separate model. 我对此的想法是,我不喜欢在部门中将部门数据存储在字段中的想法,这是一对多的关系,因此部门应该是一个单独的模型。 I would remove the department field from org. 我会从组织中删除部门字段。 Then create a migration to create a departments table. 然后创建一个迁移以创建一个Departments表。 It should look something like this: 它看起来应该像这样:

class CreateDeparments < ActiveRecord::Migration
  def change
    create_table :departments do |t|
      t.integer :org_id
      t.string :department_name

      t.timestamps
    end
  end
end

Next in the Department model add this line of code: 接下来,在Department模型中添加以下代码行:

belongs_to :org

In the org model add the following: 在组织模型中,添加以下内容:

  has_many :departments, dependent: :destroy

  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

In your controllers and views you now have the following: 在控制器和视图中,您现在具有以下内容:

@org = Org.first
# List of departments as an array for a select
@org.departments
# A comma separated string for text boxes
@org.department_list

The department_list method can now be used to display the list in a text box and also be used to post and changes back. 现在,department_list方法可用于在文本框中显示列表,也可用于发布和更改。 So you your view code just becomes this: 因此,您的视图代码就是这样:

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

You will probably need to amend your org controller by changing the create to something like this: 您可能需要通过将创建更改为以下内容来修改组织控制器:

 def create
    @org = Org.new(params[:org])

    respond_to do |format|
      if @org.save
        @org.department_list = params[:org][:department_list]
        format.html { redirect_to org_url,
                      notice: "#{@org.name} was successfully created" }
        format.json { render json: @org,
                      status: :created, location: @org }
      else
        format.html { render action: "new" }
        format.json { render json: @org.errors, status: :unprocessable_entity }
      end
    end
 end

If you are still stuck I have a complete webiste on github that you can look through. 如果您仍然被卡住,我可以在github上找到完整的网站,以进行查阅。 For you it is orgs and departments and on mysite it is people and skills or people and credits. 对您来说,它是组织和部门,在我的网站上是人员和技能或人员和信誉。 This is the link: 这是链接:

https://github.com/davesexton/CKCASTING https://github.com/davesexton/CKCASTING

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM