在 CSV 上传中添加项目
Add Project on CSV upload
您好,我有一个应用程序,其中包含项目和项目模型。
该项属于项目。
我通过 csv 导入批量导入项目。
此时 @project 被识别并在散列中调用,使用 csv 中的列。
我想更改它,以便我能够删除 csv 中的列并使用关联,因为从 @project 页面调用 csv 导入
app/models/project.rb
class Project < ApplicationRecord
has_many :items, dependent: :destroy
end
app/models/item.rb
class Item < ApplicationRecord
belongs_to :project
enum status: [:unscheduled, :scheduled, :delivered]
def self.import(file)
CSV.foreach(file.path, headers: true, header_converters: :symbol) do |row|
Item.create! row.to_hash
end
end
def self.to_csv
CSV.generate do |csv|
csv << column_names
all.each do |item|
csv << item.attributes.values_at(*column_names)
end
end
end
end
该项嵌套在项目下
app/controllers/projects_controller.rb
class ProjectsController < ApplicationController
before_action :authenticate_user!
before_action :set_project, only: [:show, :edit, :update, :destroy]
# GET /projects
# GET /projects.json
def index
@projects = current_user.projects.all
end
# GET /projects/1
# GET /projects/1.json
def show
@project = Project.find(params[:id])
@item = @project.items.new
@items = if params[:term]
@project.items.where('name LIKE ?', "%#{params[:term]}%")
else
@project.items.all
end
respond_to do |format|
format.html
format.csv { send_data @items.to_csv }
end
end
# GET /projects/new
def new
@project = Project.new
end
# GET /projects/1/edit
def edit
end
# POST /projects
# POST /projects.json
def create
@project = Project.new(project_params)
@project.project_users.new(user:current_user, role: "owner")
respond_to do |format|
if @project.save
format.html { redirect_to @project, notice: 'Project was successfully created.' }
format.json { render :show, status: :created, location: @project }
else
format.html { render :new }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /projects/1
# PATCH/PUT /projects/1.json
def update
respond_to do |format|
if @project.update(project_params)
format.html { redirect_to @project, notice: 'Project was successfully updated.' }
format.json { render :show, status: :ok, location: @project }
else
format.html { render :edit }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
# DELETE /projects/1
# DELETE /projects/1.json
def destroy
@project.destroy
respond_to do |format|
format.html { redirect_to projects_url, notice: 'Project was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_project
@project = current_user.projects.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def project_params
params.require(:project).permit(:name, :number, :street, :suburb, :state, :postcode, :country)
end
end
app/controllers/projects/items_controller.rb
class Projects::ItemsController < ApplicationController
# GET /items/new
def new
@project = Project.find(params[:project_id])
@item = Item.new
end
def index
@project = Project.find(params[:project_id])
@items = @project.items.all
respond_to do |format|
format.html
format.csv { send_data @items.to_csv }
end
end
# GET /items/1/edit
def edit
end
# POST /items
# POST /items.json
def create
@project = Project.find(params[:project_id])
@item = Item.new(item_params)
@item.project_id = @project.id
respond_to do |format|
if @item.save
format.html { redirect_to @item.project, notice: 'Item was successfully created.' }
format.json { render :show, status: :created, location: @item.project }
else
format.html { render :new }
format.json { render json: @item.project.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /items/1
# PATCH/PUT /items/1.json
def update
@item = Item.find(params[:id])
@project = Project.find(params[:project_id])
respond_to do |format|
if @item.update(item_params)
format.html { redirect_to @item.project, notice: 'Item was successfully updated.' }
format.json { render :show, status: :ok, location: @item.project }
else
format.html { render :edit }
format.json { render json: @item.project.errors, status: :unprocessable_entity }
end
end
end
# DELETE /items/1
# DELETE /items/1.json
def destroy
@item = Item.find(params[:id])
@project = Project.find(params[:project_id])
title = @item.model
if @item.destroy
flash[:notice] = "One \'#{title}' was successfully destroyed."
redirect_to @project
else
flash[:notice] = "Error Yo"
render :show
end
end
def import
Item.import(params[:file])
redirect_to projects_path(@project), notice: "Sucessfully Added Items"
end
private
# Use callbacks to share common setup or constraints between actions.
# Never trust parameters from the scary internet, only allow the white list through.
def item_params
params.require(:item).permit(:model, :project_id, :name, :search)
end
end
您会注意到我有一个 csv 导入,如果上传文件包含 project_id.
item.csv
model project_id name
A 8 1
app/views/projects/show.html.erb
<%= form_tag import_project_items_path(@project) , multipart: true do%>
<div class="custom-file">
<%= file_field_tag :file, placeholder: 'Add Your File', required: true, class: 'custom-file-input', id: 'customFile' %>
<label class="custom-file-label" for="customFile">Choose file</label>
</div>
<div class="form-group mt-2">
<%= submit_tag "Upload Items", class: 'btn btn-primary btn-block' %>
</div>
<% end %>
所以我的问题是如何将@project 添加到项目项导入
def import
Item.import(params[:file])
redirect_to projects_path(@project), notice: "Sucessfully Added Items"
end
def self.import(file)
CSV.foreach(file.path, headers: true, header_converters: :symbol) do |row|
Item.create! row.to_hash
end
end
您必须在 routes.rb
中创建路径
get 'projects/:project_id/items/:id/import' => 'items#import', as: :items_import
然后您可以从任何地方(比如从展示页面)简单地调用
items_import_path(@project, @item)
上面写出我的代码的过程帮助我意识到。
我只需要更改导入中的一行
来自
def import
Item.import(params[:file])
redirect_to projects_path(@project), notice: "Sucessfully Added Items"
end
至
def import
@project = Project.find(params[:project_id])
@project.items.import(params[:file])
redirect_to projects_path(@project), notice: "Sucessfully Imported Items!"
end
您好,我有一个应用程序,其中包含项目和项目模型。
该项属于项目。
我通过 csv 导入批量导入项目。
此时 @project 被识别并在散列中调用,使用 csv 中的列。
我想更改它,以便我能够删除 csv 中的列并使用关联,因为从 @project 页面调用 csv 导入
app/models/project.rb
class Project < ApplicationRecord
has_many :items, dependent: :destroy
end
app/models/item.rb
class Item < ApplicationRecord
belongs_to :project
enum status: [:unscheduled, :scheduled, :delivered]
def self.import(file)
CSV.foreach(file.path, headers: true, header_converters: :symbol) do |row|
Item.create! row.to_hash
end
end
def self.to_csv
CSV.generate do |csv|
csv << column_names
all.each do |item|
csv << item.attributes.values_at(*column_names)
end
end
end
end
该项嵌套在项目下
app/controllers/projects_controller.rb
class ProjectsController < ApplicationController
before_action :authenticate_user!
before_action :set_project, only: [:show, :edit, :update, :destroy]
# GET /projects
# GET /projects.json
def index
@projects = current_user.projects.all
end
# GET /projects/1
# GET /projects/1.json
def show
@project = Project.find(params[:id])
@item = @project.items.new
@items = if params[:term]
@project.items.where('name LIKE ?', "%#{params[:term]}%")
else
@project.items.all
end
respond_to do |format|
format.html
format.csv { send_data @items.to_csv }
end
end
# GET /projects/new
def new
@project = Project.new
end
# GET /projects/1/edit
def edit
end
# POST /projects
# POST /projects.json
def create
@project = Project.new(project_params)
@project.project_users.new(user:current_user, role: "owner")
respond_to do |format|
if @project.save
format.html { redirect_to @project, notice: 'Project was successfully created.' }
format.json { render :show, status: :created, location: @project }
else
format.html { render :new }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /projects/1
# PATCH/PUT /projects/1.json
def update
respond_to do |format|
if @project.update(project_params)
format.html { redirect_to @project, notice: 'Project was successfully updated.' }
format.json { render :show, status: :ok, location: @project }
else
format.html { render :edit }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
# DELETE /projects/1
# DELETE /projects/1.json
def destroy
@project.destroy
respond_to do |format|
format.html { redirect_to projects_url, notice: 'Project was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_project
@project = current_user.projects.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def project_params
params.require(:project).permit(:name, :number, :street, :suburb, :state, :postcode, :country)
end
end
app/controllers/projects/items_controller.rb
class Projects::ItemsController < ApplicationController
# GET /items/new
def new
@project = Project.find(params[:project_id])
@item = Item.new
end
def index
@project = Project.find(params[:project_id])
@items = @project.items.all
respond_to do |format|
format.html
format.csv { send_data @items.to_csv }
end
end
# GET /items/1/edit
def edit
end
# POST /items
# POST /items.json
def create
@project = Project.find(params[:project_id])
@item = Item.new(item_params)
@item.project_id = @project.id
respond_to do |format|
if @item.save
format.html { redirect_to @item.project, notice: 'Item was successfully created.' }
format.json { render :show, status: :created, location: @item.project }
else
format.html { render :new }
format.json { render json: @item.project.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /items/1
# PATCH/PUT /items/1.json
def update
@item = Item.find(params[:id])
@project = Project.find(params[:project_id])
respond_to do |format|
if @item.update(item_params)
format.html { redirect_to @item.project, notice: 'Item was successfully updated.' }
format.json { render :show, status: :ok, location: @item.project }
else
format.html { render :edit }
format.json { render json: @item.project.errors, status: :unprocessable_entity }
end
end
end
# DELETE /items/1
# DELETE /items/1.json
def destroy
@item = Item.find(params[:id])
@project = Project.find(params[:project_id])
title = @item.model
if @item.destroy
flash[:notice] = "One \'#{title}' was successfully destroyed."
redirect_to @project
else
flash[:notice] = "Error Yo"
render :show
end
end
def import
Item.import(params[:file])
redirect_to projects_path(@project), notice: "Sucessfully Added Items"
end
private
# Use callbacks to share common setup or constraints between actions.
# Never trust parameters from the scary internet, only allow the white list through.
def item_params
params.require(:item).permit(:model, :project_id, :name, :search)
end
end
您会注意到我有一个 csv 导入,如果上传文件包含 project_id.
item.csv
model project_id name
A 8 1
app/views/projects/show.html.erb
<%= form_tag import_project_items_path(@project) , multipart: true do%>
<div class="custom-file">
<%= file_field_tag :file, placeholder: 'Add Your File', required: true, class: 'custom-file-input', id: 'customFile' %>
<label class="custom-file-label" for="customFile">Choose file</label>
</div>
<div class="form-group mt-2">
<%= submit_tag "Upload Items", class: 'btn btn-primary btn-block' %>
</div>
<% end %>
所以我的问题是如何将@project 添加到项目项导入
def import
Item.import(params[:file])
redirect_to projects_path(@project), notice: "Sucessfully Added Items"
end
def self.import(file)
CSV.foreach(file.path, headers: true, header_converters: :symbol) do |row|
Item.create! row.to_hash
end
end
您必须在 routes.rb
中创建路径get 'projects/:project_id/items/:id/import' => 'items#import', as: :items_import
然后您可以从任何地方(比如从展示页面)简单地调用
items_import_path(@project, @item)
上面写出我的代码的过程帮助我意识到。
我只需要更改导入中的一行
来自
def import
Item.import(params[:file])
redirect_to projects_path(@project), notice: "Sucessfully Added Items"
end
至
def import
@project = Project.find(params[:project_id])
@project.items.import(params[:file])
redirect_to projects_path(@project), notice: "Sucessfully Imported Items!"
end