将上传的CSV文件中的行与rails中的用户相关联 [英] Associating rows from uploaded CSV files with a User in rails
问题描述
我要关注 http://railscasts.com上半年/ episodes / 396-importing-csv-and-excel 上传CSV文件并在我的rails应用程序中创建新的示例。
以创建仅呈现当前用户样本的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
如果params [:file] .present?
Sample.import(params [:file],params [:user_id])
redirect_to root_url,通知:样品导入
else
redirect_to root_url,注意:首先选择一个文件!
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
b $ b def update
if @ sample.update(sample_params)
redirect_to @sample,notice:'Sample was successfully updated。'
else
render action:'edit'
end
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
end
Sample.rb模型
ActiveRecord :: Base
belongs_to:userdef 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>行< / strong>
<%= sample.line%>
< / div>
< div class =col-md-4>
< strong>季节< / 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>颜色< / strong>
<%= sample.color%>
< / div>
< div class =col-md-4>
< strong>日期输出< / strong>
<%= sample.date_out%>
< / div>
< div class =col-md-4>
< strong>给谁< / strong>
<%= sample.to_who%>
< / div>
< / div>
< div class =row>
< div class =col-md-4>
< strong>日期在< / strong>
<%= sample.date_in%>
< / div>
< div class =col-md-4>
< strong>位置< / strong>
<%= sample.location%>
< / div>
< div class =col-md-4 bottom>
<%= link_to'签入| out',edit_sample_path(sample),class:btn btn-md btn-default%>
< / div>
< / div>
<%end%>
<%end%>
< / div>
EDIT 我使用byebug来遍历模型,任何错误。但是最后一次我做了,我退出byebug,关闭服务器,重新启动它,并刷新此网址 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)
如果用户
CSV.foreach(file.path,headers:true)do | row |
user.samples.create! row.to_hash
end
end
end
end
I'm following the first half of http://railscasts.com/episodes/396-importing-csv-and-excel to upload CSV files and create new "samples" in my rails app.
I'm trying to create a mysamples page that renders only the current user's samples. So far if I add a sample one at a time through a form it will show up on this page.
But when I upload the CSV the new samples aren't getting associated with the user.
Any suggestions for the best way to do this?
Thanks for your help.
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
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
end
Sample.rb model
class Sample < ActiveRecord::Base belongs_to :user
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 I used byebug to step through the model and haven't gotten any errors. But the last time I did it, I quit byebug, shut down the server, the restarted it and hit refresh on this url http://localhost:3000/samples/import
This led to an error I hadn't seen.
解决方案You're not passing in the user id so merge the user_id onto your hash:
def self.import(file, user_id) CSV.foreach(file.path, headers: true) do |row| Sample.import(params[:file], current_user.id) end end end
You could find the user and do it through an association to ensure the user exists - how you want to deal with it not existing I leave to you. Using find_by so it doesn't throw an active record not found error, if you want that then just use find instead.
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
这篇关于将上传的CSV文件中的行与rails中的用户相关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!