Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6662785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:25:40+00:00 2026-05-26T02:25:40+00:00

I’m trying to create a form using textarea and a submit button that will

  • 0

I’m trying to create a form using textarea and a submit button that will allow users to do bulk insert. For example, the input would look like this:

0001;MR A
0002;MR B

The result would look like this:

mysql> select * from members;

+------+------+------+
|  id  |  no  | name |
+------+------+------+
|   1  | 0001 | MR A |
+------+------+------+
|   2  | 0002 | MR B |
+------+------+------+

I’m very new to Rails and I’m not sure on how to proceed with this one. Should I use attr_accessor? How do I handle failed validations in the form view? Is there any example? Thanks in advance.

Update

Based on MissingHandle‘s comment, I created a Scaffold and replace the Model’s code with this:

class MemberBulk < ActiveRecord::Base

  attr_accessor :member

  def self.columns
    @columsn ||= []
  end

  def self.column(name, sql_type = nil, default = nil, null = true)
    columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
  end

  column :data, :text

  validates :data, :create_members, :presence => true

  def create_members
    rows = self.data.split("\r\n")

    @member = Array.new

    rows.each_with_index { |row, i|
      rows[i] = row.strip
      cols = row.split(";")

      p = Member.new
      p.no = cols[0]
      p.name = cols[1]

      if p.valid?
        member << p
      else
        p.errors.map { |k, v| errors.add(:data, "\"#{row}\" #{v}") }
      end
    }
  end

  def create_or_update
    member.each { |p|
      p.save
    }
  end
end

I know the code is far from complete, but I need to know is this the correct way to do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:25:40+00:00Added an answer on May 26, 2026 at 2:25 am

    In the end I had to change from using Abstract Class to Active Model (not sure why, but it stoppped working the moment I upgrade to Rails v3.1). Here’s the working code:

    class MemberBulk
      include ActiveModel::Validations
      include ActiveModel::Conversion
      extend ActiveModel::Naming
    
      attr_accessor :input, :data
    
      validates :input, presence: true
    
      def initialize(attributes = {})no
        attributes.each do |name, value|
          send("#{name}=", value) if respond_to?("#{name}=")
        end
      end
    
      def persisted?
        false
      end  
    
      def save
        unless self.valid?
          return false
        end
    
        data = Array.new
    
        # Check for spaces
        input.strip.split("\r\n").each do |i|
          if i.strip.empty?
            errors.add(:input, "There shouldn't be any empty lines")
          end
    
          no, nama = i.strip.split(";")
    
          if no.nil? or nama.nil?
            errors.add(:input, "#{i} doesn't have no or name")
          else
            no.strip!
            nama.strip!
    
            if no.empty? or nama.empty?
              errors.add(:input, "#{i} doesn't have no or name")
            end
          end  
    
          p = Member.new(no: no, nama: nama)
          if p.valid?
            data << p
          else
            p.errors.full_messages.each do |error|
              errors.add(:input, "\"#{i}\": #{error}")  
            end        
          end             
        end # input.strip    
    
        if errors.empty?
          if data.any?
    
            begin
              data.each do |d|
                d.save
              end
            rescue Exception => e
              raise ActiveRecord::Rollback
            end
    
          else
            errors.add(:input, "No data to be processed")
            return false
          end
        else
          return false
        end
    
      end # def
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.