简体   繁体   中英

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? Each line in the text box or separated by a comma should be a separate record.

How would the controller look?

_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

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

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.

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.

Let's say you use a new line to delineate products so your text area looks like:

product1
product2
product3

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. If you used split('\\n') it won't work.

I would also look at wrapping it in a transaction if you want to make sure they all save.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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