Rails 4: Validate file, then import if conditions met -
i importing file using smartercsv , have 1 function validates aspects of file, , redirects you, displaying results.
i display include button says "import", allows import same file long satisfied displayed.
how can pass file second function after redirect without having select file again?
# validate file , check display def check_file @success, @error = timecard.check_timecard(params[:file]) redirect_to timecards_path, notice: [@success, @error] end #import database if display satisfactory def bulk_upload x = timecard.import(params[:file]) redirect_to timecards_path, notice: "times imported successfully." end #view showing display <% if flash[:notice].is_a? string %> <%= flash[:notice] %> <% elsif flash[:notice].is_a? array %> <div class="container"> <div class="col-xs-10 col-xs-offset-1"> <table class="table table-hover table-striped table-bordered"> <thead> <tr> <th>name</th> <th>regular</th> <th>overtime</th> <th>sick</th> <th>vacation</th> <th>holiday</th> </tr> </thead> <tbody> <% notice[0].each |success| %> <tr> <td style="color: green"><%= success[0] %></td> <% success[1].each |type| %> <td><%= type %></td> <% end %> </tr> <% end %> <% notice[1].each |failure| %> <tr> <td style="color: red"><%= failure[0] %></td> <td><%= success[1] %></td> </tr> <% end %> </tbody> </table> </div> <div class="container-fluid"> <div class="col-xs-12 col-md-6 col-md-offset-3 text-xs-center"> <div class="card card-nav-tabs"> <div class="header header-info"> import timecard , send email </div> <div class="content"> <!-- import goes here --> </div> </div> </div> </div> </div> <% end %>
there nice way this. should move file location within application , then, store path of file in database against record trying upload file to. consider following example. assuming uploading file timecard record , instance exist @time_card
name = params[:upload][:file].original_filename directory = "public/images/upload" path = file.join(directory, name) file.open(path, "wb") { |f| f.write(params[:upload][:file].read) } @time_card.csv_path = path
then, redirect user validation error page. then, after use submits page can import csv using file in @time_card.csv_path
Comments
Post a Comment