Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ####parse csv####
- class Editor::CsvController < ApplicationController
- before_filter :require_author_or_editor
- layout 'editor'
- def index
- #just to call the index view...
- end
- def parse
- @parsed_file = (params[:dump][:file])
- n = 0
- @parsed_file.each do |big_file|
- big_file.split("\r").each do |line|
- user_array = line.split(",")
- user = User.new
- user_name = user_array[0] +" "+ user_array[1]
- user_pass = user_name.reverse.gsub(/\s/,'')
- user.password = user_pass
- user.password_confirmation = user_pass
- user.name = user_name
- user.login = user_name.downcase.gsub(/\s/,'')
- user.email = user_array[2].strip
- if user.save
- user.activate!
- author = Author.new
- author.user_id = user.id
- author.short_bio = user_array[3..-1].join(",")
- author.save!
- end
- if user.valid? && author.valid?
- n=n+1
- end
- end
- end
- flash.now[:message]="CSV Import Successful, #{n} new authors created."
- redirect_to '/editor/csv'
- end
- end
Add Comment
Please, Sign In to add comment