gpt4 book ai didi

ruby-on-rails - 形式验证?

转载 作者:数据小太阳 更新时间:2023-10-29 08:50:14 25 4
gpt4 key购买 nike

我正在尝试进行验证,但在上传 excel 时没有找到正确的方法

查看代码

<%= form_for :dump, :url=>  import_csv_index_path, :html => { :multipart => true } do |f| %>
<% if (-----).errors.any? %>
<div id="error_explanation">
<%= pluralize((----).errors.count, "error") %> prohibited this link from being saved:

<% (---).errors.full_messages.each do |msg| %>
<li><%= msg %></li>
</div>

<% end %>

<%= f.label :'Select an Excel File:' %>
<%= f.file_field :file %>
<i href="#" id="blob_1" class="icon-info-sign" rel="popover" style="margin-top: 300px"></i><br/>

<%= submit_tag 'Submit' ,:id => 'submit',:class =>"btn btn-primary"%><br/><br/>

<% end -%>

模型代码:

class Person < ActiveRecord::Base

attr_accessible :date, :email, :name, :avatar ,:template_id

validates_presence_of :email,:message => 'Email is compulsory'

end

我在 (-----) 的地方写什么

谢谢

最佳答案

我已经完成了其他方法,所以在这里讨论,

Controller 代码:

 def import

@error = []
@error = Person.import(params[:dump][:file])
redirect_to people_path, notice: "Employees imported.",:flash => { :error => "Employees( Email : invalid/blank) are not saved : #{@error.join(" , ")}" }
end

在模型中:

def self.import(文件)

 spreadsheet = open_spreadsheet(file)
@err = []
@alt = []

header = spreadsheet.row(1)
(2..spreadsheet.last_row).each do |i|
row = Hash[[header, spreadsheet.row(i)].transpose]
@person = Person.find_by_id(row["id"]) ||Person.new



@person.name = row["Name"].to_s
@person.date = row["Date"]
@person.email = row["Email"]



if @person.save

else
@err << {:name => row["Name"].to_s,:error => @person.errors.messages}
end

end
@err.each do |t|



@alt.push(t[:name])


end

return @alt

end

并将其称为 flash 消息...现在工作正常。

谢谢

关于ruby-on-rails - 形式验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14459389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com