将用户 .csv 文件中的数据导入 table
Import data from user .csv file into table
在 Rails 网络应用程序的 Ruby 中,我想从用户导入一个 .csv 文件,并且 .csv 文件中存在的数据将被插入到 questions
table,但是当我 运行 应用程序时出现错误。
当我删除在 question.rb
和 html.erb
文件中编写的代码时,应用程序运行正常。
控制器代码:
questions_controller.rb
class QuestionsController < ApplicationController
filter_access_to :all
def import
@question = Question.new
@question.import(params[:file])
CSV.foreach(file.path, headers: true) do |row|
@question.save! row.to_hash
end
redirect_to root_url, notice: "Questions imported succefully."
end
end
型号代码:
question.rb
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
@question.save! row.to_hash
end
end
查看代码:
question_type_listing.html.erb
<fieldset class="formContainer">
<legend><%=t('question.select_qtype_cat')%></legend>
<%= form_tag({:action => "import"}, {:id => "class_form"}, multipart: true) do %>
<span style="width: 130px; float: left;"><p><label for="upload_file">Select File</label> :</span>
<%= file_field_tag :file %>
<%= submit_tag "Import CSV" %>
<% end %>
</fieldset>
有人可以建议解决此问题的方法吗?
案例一:
如果你想把所有问题都保存在.csv中,只需要下面的控制器questions_controller.rb
代码就可以实现,不需要question.rb
函数import
:
def import
file = params[:file]
CSV.foreach(file.path, headers: true) do |row|
@question = Question.new
@question.save! row.to_hash
end
redirect_to root_url, notice: "Questions imported succefully."
end
案例二:
如果你希望代码在模型中(Rails约定),写控制器questions_controller.rb
:
def import
Question.import(params[:file])
redirect_to root_url, notice: "Questions imported succefully."
end
和模特question.rb
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
question = Question.new
question.save! row.to_hash
end
end
在 Rails 网络应用程序的 Ruby 中,我想从用户导入一个 .csv 文件,并且 .csv 文件中存在的数据将被插入到 questions
table,但是当我 运行 应用程序时出现错误。
当我删除在 question.rb
和 html.erb
文件中编写的代码时,应用程序运行正常。
控制器代码: questions_controller.rb
class QuestionsController < ApplicationController
filter_access_to :all
def import
@question = Question.new
@question.import(params[:file])
CSV.foreach(file.path, headers: true) do |row|
@question.save! row.to_hash
end
redirect_to root_url, notice: "Questions imported succefully."
end
end
型号代码: question.rb
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
@question.save! row.to_hash
end
end
查看代码: question_type_listing.html.erb
<fieldset class="formContainer">
<legend><%=t('question.select_qtype_cat')%></legend>
<%= form_tag({:action => "import"}, {:id => "class_form"}, multipart: true) do %>
<span style="width: 130px; float: left;"><p><label for="upload_file">Select File</label> :</span>
<%= file_field_tag :file %>
<%= submit_tag "Import CSV" %>
<% end %>
</fieldset>
有人可以建议解决此问题的方法吗?
案例一:
如果你想把所有问题都保存在.csv中,只需要下面的控制器questions_controller.rb
代码就可以实现,不需要question.rb
函数import
:
def import
file = params[:file]
CSV.foreach(file.path, headers: true) do |row|
@question = Question.new
@question.save! row.to_hash
end
redirect_to root_url, notice: "Questions imported succefully."
end
案例二:
如果你希望代码在模型中(Rails约定),写控制器questions_controller.rb
:
def import
Question.import(params[:file])
redirect_to root_url, notice: "Questions imported succefully."
end
和模特question.rb
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
question = Question.new
question.save! row.to_hash
end
end