简体   繁体   English

Ruby on Rails:嵌套属性,在单个文本区域中保存多个记录

[英]Ruby on Rails: Nested Attributes, saving multiple records in single text area

How do I save multiple records with nested attributes using single text_area? 如何使用单个text_area保存具有嵌套属性的多个记录? Each line in the text box or separated by a comma should be a separate record. 文本框中的每一行或用逗号分隔的每一行都应是单独的记录。

How would the controller look? 控制器的外观如何?

_form.html.erb _form.html.erb

<%= simple_form_for @project do |f| %>

  <%= f.simple_fields_for :products do |g| %>
    <%= render 'product_fields', :f => g %>
  <% end%>
  <%= link_to_add_association 'add item', f, :products %>

<% end %>

_product_fields.html.erb _product_fields.html.erb

<%= f.text_field :category, placeholder: "Category" %>
<%= f.text_area :item, placeholder: "List your products (separated by each line or comma)" %>

project_controller.rb project_controller.rb

def create
  @project = Project.new(project_params)

  respond_to do |format|
    format.js

    if @project.save

      format.html { redirect_to @project, notice: 'Project was successfully created.' }
      format.json { render :show, status: :created, location: @project }
    else
      format.html { render :new }
      format.json { render json: @project.errors, status: :unprocessable_entity }
    end
  end
end


def project_params
  params.require(:project).permit(
    :user_id,  
    products_attributes: [:id, :item, :hyperlink, :_destroy, :category]).merge(user_id: current_user.id)
end

I would like to go into my project form, and then there's a large text_area where I can add a list of products, and each product (separated by "enter" or a "comma") will be a record. 我想进入我的项目表单,然后有一个很大的text_area,可以在其中添加产品列表,并且每个产品(用“ enter”或“逗号”分隔)都是一个记录。

EDIT ---- 编辑----

Adding Models: 添加模型:

class Project < ActiveRecord::Base
  has_many :products, dependent: :destroy
  accepts_nested_attributes_for :products, :reject_if => :all_blank, allow_destroy: true
end

class Product < ActiveRecord::Base
  belongs_to :project
end

Normally you save your params without much manipulation. 通常,您无需太多操作即可保存参数。 If you want to turn your text_area param into multiple records just chop it up and process it in the controller. 如果要将text_area参数转换为多个记录,只需将其切碎并在控制器中进行处理即可。

Let's say you use a new line to delineate products so your text area looks like: 假设您使用新行来描述产品,因此您的文本区域如下所示:

product1 产品1
product2 产品2
product3 产品3

project_params[:product_list] = "product1\nproduct2\nproduct3"
prod_arr = project_params[:product_list].split("\n")
prod_arr.each do |product|
  #you now have your product name in the local variable product
  #you can now save each one separately. You will probably
  #need to take common items of the params hash and insert the 
  #current product, then save.
end

You can choose to split on just about any character. 您可以选择分割几乎所有字符。 But pick one that makes sense and then also apply some sort of checking on the string you are splitting. 但是,选择一个有意义的选项,然后对要拆分的字符串进行某种检查。 Notice the split("\\n") is using double quotes, that is needed to tell Ruby that you are talking about the newline character. 注意split("\\n")使用双引号,这是告诉Ruby您正在谈论换行符所必需的。 If you used split('\\n') it won't work. 如果使用split('\\n') ,它将无法正常工作。

I would also look at wrapping it in a transaction if you want to make sure they all save. 如果您想确保它们全部保存,我还将考虑将其包装在事务中。

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

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