Rails 4:验证文件,然后在满足条件时导入 [英] Rails 4: Validate file, then import if conditions met

查看:160
本文介绍了Rails 4:验证文件,然后在满足条件时导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SmarterCSV导入文件,我有1个功能验证文件的某些方面,然后重定向您,显示结果。

I am importing a file using SmarterCSV and I have 1 function that validates certain aspects of the file, and then redirects you, displaying the results.

我想要此显示还包括一个显示导入的按钮,只要您对显示的内容感到满意,就可以导入同一个文件。

I would like this display to also include a button that says "import", which allows you to import that same file as long as you are satisfied with what is displayed.

如何在重定向后将文件传递给第二个函数而无需再次选择文件?

# validate file and check display
def check_file
    @success, @error = Timecard.check_timecard(params[:file])
    redirect_to timecards_path, notice: [@success, @error]
end

#import into database if display is 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 do |success| %>
                    <tr>
                        <td style="color: green"><%= success[0] %></td>
                        <% success[1].each do |type| %>
                        <td><%= type %></td>
                        <% end %>
                    </tr>
                    <% end %>
                    <% notice[1].each do |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 and Send Email
                    </div>
                    <div class="content">

                      <!-- IMPORT GOES HERE -->

                    </div>
                </div>
            </div>
        </div>
    </div>
<% end %>


推荐答案

有一种很好的方法可以做到这一点。您应该将文件移动到应用程序中的某个位置,然后将该文件的路径存储在您尝试将此文件上载到的记录中。请考虑以下示例。假设您要将此文件上传到TimeCard记录,并且实例存在为 @time_card

There is a nice way to do this. You should move the file to a location within your application and then, store the path of the file in the database against the record you are trying to upload this file to. Consider the following example. Assuming you are uploading this file to a TimeCard record and an instance exist as @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

然后,将用户重定向到验证错误页面。然后,在使用提交页面后,您可以使用 @ time_card.csv_path

Then, redirect the user to the validation error page. Then, after use submits the page you can import the CSV using the file in @time_card.csv_path

这篇关于Rails 4:验证文件,然后在满足条件时导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆