将上传的 CSV 文件中的行与 rails 中的用户相关联
Associating rows from uploaded CSV files with a User in rails
我正在按照 http://railscasts.com/episodes/396-importing-csv-and-excel 的前半部分上传 CSV 文件并在我的 rails 应用程序中创建新的 "samples"。
我正在尝试创建一个仅呈现当前用户示例的 mysamples 页面。到目前为止,如果我通过表格一次添加一个示例,它将显示在此页面上。
但是当我上传 CSV 文件时,新样本没有与用户相关联。
有什么关于最好的方法的建议吗?
感谢您的帮助。
Sample_Controller.rb
class SamplesController < ApplicationController
before_action :set_sample, only: [:show, :edit, :update, :destroy]
after_action :verify_authorized
respond_to :html
def home
@samples = Sample.all
authorize @samples
end
def admin_only
end
def index
@q = Sample.search(params[:q])
@samples = @q.result(distinct: true).order("created_at DESC").paginate(:page => params[:page], :per_page => 10 )
respond_with(@samples)
authorize @samples
end
def mysample
@samples = Sample.all
authorize @samples
end
def import
@samples = Sample.all
if params[:file].present?
Sample.import(params[:file], params[:user_id])
redirect_to root_url, notice: "Samples Imported"
else
redirect_to root_url, notice: "You need to choose a file first!"
end
authorize @samples
end
def show
respond_with(@sample)
end
def new
@sample = Sample.new
respond_with(@sample)
authorize @sample
end
def edit
end
def create
params[:sample][:user_id] = current_user.id
@sample = Sample.new(sample_params)
authorize @sample
if @sample.save
redirect_to @sample, notice: 'Sample was successfully created.'
else
render action: 'new'
end
end
def update
if @sample.update(sample_params)
redirect_to @sample, notice: 'Sample was successfully updated.'
else
render action: 'edit'
end
结束
def destroy
@sample.destroy
authorize @sample
respond_with(@sample)
end
private
def set_sample
@sample = Sample.find(params[:id])
authorize @sample
end
def sample_params
params.require(:sample).permit(:line, :season, :style, :color, :date_out, :to_who, :date_in, :location,:user_id)
end
结束
Sample.rb 型号
class 样本 < ActiveRecord::Base
belongs_to:用户
def self.import(file, user_id)
CSV.foreach(file.path, headers: true) do |row|
Sample.create! row.to_hash
end
end
end
mysample.html.erb
<div class="container">
<% @samples.each do |sample| %>
<% if sample.user == current_user %>
<div class="row sample">
<div class="col-md-4">
<strong>Line</strong>
<%= sample.line %>
</div>
<div class="col-md-4">
<strong>Season</strong>
<%= sample.season %>
</div>
<div class="col-md-4">
<strong>Style</strong>
<%= sample.style %>
</div>
</div>
<div class="row">
<div class="col-md-4">
<strong>Color</strong>
<%= sample.color%>
</div>
<div class="col-md-4">
<strong>Date Out</strong>
<%= sample.date_out %>
</div>
<div class="col-md-4">
<strong>To Who</strong>
<%= sample.to_who %>
</div>
</div>
<div class="row">
<div class="col-md-4">
<strong>Date In</strong>
<%= sample.date_in %>
</div>
<div class="col-md-4">
<strong>Location</strong>
<%= sample.location %>
</div>
<div class="col-md-4 bottom">
<%= link_to 'Check In | Out', edit_sample_path(sample), class: "btn btn-md btn-default" %>
</div>
</div>
<% end %>
<% end %>
</div>
EDIT 我使用 byebug 单步调试模型,没有出现任何错误。但是上次我这样做时,我退出了 byebug,关闭了服务器,然后重新启动它并在此 url http://localhost:3000/samples/import
上刷新
这导致了一个我没见过的错误。
您没有传递用户 ID,因此请将 user_id 合并到您的散列中:
def self.import(file, user_id)
CSV.foreach(file.path, headers: true) do |row|
Sample.import(params[:file], current_user.id)
end
end
end
您可以找到用户并通过关联来确保用户存在——您希望如何处理不存在的用户,我留给您。使用 find_by 这样它就不会抛出未找到活动记录的错误,如果您需要,则只需使用 find 即可。
def self.import(file, user_id)
user = User.find_by(id: user_id)
if user
CSV.foreach(file.path, headers: true) do |row|
user.samples.create! row.to_hash
end
end
end
end
我正在按照 http://railscasts.com/episodes/396-importing-csv-and-excel 的前半部分上传 CSV 文件并在我的 rails 应用程序中创建新的 "samples"。
我正在尝试创建一个仅呈现当前用户示例的 mysamples 页面。到目前为止,如果我通过表格一次添加一个示例,它将显示在此页面上。
但是当我上传 CSV 文件时,新样本没有与用户相关联。
有什么关于最好的方法的建议吗?
感谢您的帮助。
Sample_Controller.rb
class SamplesController < ApplicationController
before_action :set_sample, only: [:show, :edit, :update, :destroy]
after_action :verify_authorized
respond_to :html
def home
@samples = Sample.all
authorize @samples
end
def admin_only
end
def index
@q = Sample.search(params[:q])
@samples = @q.result(distinct: true).order("created_at DESC").paginate(:page => params[:page], :per_page => 10 )
respond_with(@samples)
authorize @samples
end
def mysample
@samples = Sample.all
authorize @samples
end
def import
@samples = Sample.all
if params[:file].present?
Sample.import(params[:file], params[:user_id])
redirect_to root_url, notice: "Samples Imported"
else
redirect_to root_url, notice: "You need to choose a file first!"
end
authorize @samples
end
def show
respond_with(@sample)
end
def new
@sample = Sample.new
respond_with(@sample)
authorize @sample
end
def edit
end
def create
params[:sample][:user_id] = current_user.id
@sample = Sample.new(sample_params)
authorize @sample
if @sample.save
redirect_to @sample, notice: 'Sample was successfully created.'
else
render action: 'new'
end
end
def update
if @sample.update(sample_params)
redirect_to @sample, notice: 'Sample was successfully updated.'
else
render action: 'edit'
end
结束
def destroy
@sample.destroy
authorize @sample
respond_with(@sample)
end
private
def set_sample
@sample = Sample.find(params[:id])
authorize @sample
end
def sample_params
params.require(:sample).permit(:line, :season, :style, :color, :date_out, :to_who, :date_in, :location,:user_id)
end
结束
Sample.rb 型号
class 样本 < ActiveRecord::Base belongs_to:用户
def self.import(file, user_id)
CSV.foreach(file.path, headers: true) do |row|
Sample.create! row.to_hash
end
end
end
mysample.html.erb
<div class="container">
<% @samples.each do |sample| %>
<% if sample.user == current_user %>
<div class="row sample">
<div class="col-md-4">
<strong>Line</strong>
<%= sample.line %>
</div>
<div class="col-md-4">
<strong>Season</strong>
<%= sample.season %>
</div>
<div class="col-md-4">
<strong>Style</strong>
<%= sample.style %>
</div>
</div>
<div class="row">
<div class="col-md-4">
<strong>Color</strong>
<%= sample.color%>
</div>
<div class="col-md-4">
<strong>Date Out</strong>
<%= sample.date_out %>
</div>
<div class="col-md-4">
<strong>To Who</strong>
<%= sample.to_who %>
</div>
</div>
<div class="row">
<div class="col-md-4">
<strong>Date In</strong>
<%= sample.date_in %>
</div>
<div class="col-md-4">
<strong>Location</strong>
<%= sample.location %>
</div>
<div class="col-md-4 bottom">
<%= link_to 'Check In | Out', edit_sample_path(sample), class: "btn btn-md btn-default" %>
</div>
</div>
<% end %>
<% end %>
</div>
EDIT 我使用 byebug 单步调试模型,没有出现任何错误。但是上次我这样做时,我退出了 byebug,关闭了服务器,然后重新启动它并在此 url http://localhost:3000/samples/import
上刷新这导致了一个我没见过的错误。
您没有传递用户 ID,因此请将 user_id 合并到您的散列中:
def self.import(file, user_id)
CSV.foreach(file.path, headers: true) do |row|
Sample.import(params[:file], current_user.id)
end
end
end
您可以找到用户并通过关联来确保用户存在——您希望如何处理不存在的用户,我留给您。使用 find_by 这样它就不会抛出未找到活动记录的错误,如果您需要,则只需使用 find 即可。
def self.import(file, user_id)
user = User.find_by(id: user_id)
if user
CSV.foreach(file.path, headers: true) do |row|
user.samples.create! row.to_hash
end
end
end
end