app/controllers/import_controller.rb
# import new users without assigning a list to them
import new users without assigning a list to them
import new users without assigning a list to them
def new_users 
    authorize! :import, :new_users
    if params[:new_users] 
      if !(file = params[:new_users][:file])
        flash.now[:alert] = "Bitte wähle eine Datei."
        render :index
        return
      end
      begin
        @sheet = open_spreadsheet file
      rescue Exception => e
        flash.now[:alert] = "e.message"
        render :index
        return
      end
      @users = []
      @sheet.drop(1).each do |line|
        u = User.new(first_name: line[0], name: line[1], email: line[2].downcase)
      @users << u
    end
    elsif params[:confirm]
      users_params = JSON.parse params[:users]
      @users = []
      successful_saved = 0
      users_params.each do |user_params|
        u = User.new user_params
        @users << u
        if u.save
          successful_saved += 1
        end
      end
      flash[:notice] = "Import abgeschlossen. #{successful_saved} von #{@users.count} Benutzern erfolgreich importiert."
      render :index
    else
      redirect_to import_url
    end
  end
def new_users authorize! :import , :new_users if params [ :new_users ] if ! ( file = params [ :new_users ] [ :file ] ) flash . now [ :alert ] = "Bitte wähle eine Datei." render :index return end begin @sheet = open_spreadsheet file rescue Exception => e flash . now [ :alert ] = "e.message" render :index return end @users = [ ] @sheet . drop ( 1 ) . each do | line | u = User . new ( first_name : line [ 0 ] , name : line [ 1 ] , email : line [ 2 ] . downcase ) @users << u end elsif params [ :confirm ] users_params = JSON . parse params [ :users ] @users = [ ] successful_saved = 0 users_params . each do | user_params | u = User . new user_params @users << u if u . save successful_saved += 1 end end flash [ :notice ] = "Import abgeschlossen. #{successful_saved} von #{@users.count} Benutzern erfolgreich importiert." render :index else redirect_to import_url end end
import new users without assigning a list to them
import new users without assigning a list to them
## file upload -> do validation