Ruby on rails 轨道4+;Carrierwave+;jQuery文件上传+;嵌套表单多文件问题
我正在使用的rails应用程序有一个小问题。我正在使用Carrierwave、嵌套表单、简单表单和Jquery文件上传gems 除数据外,大部分工作正常 我有两个模型,一个项目模型和一个附件模型 提交项目表单时,所有文件都会按其应有的方式上载,附件模型中会按应有的方式创建一条记录(每个文件一条记录)。但是对于projects模型,也会为每个文件创建一个记录 我似乎不知道(在一个表单上,一次提交)如何只获取项目的一条记录和附件的多条记录 任何帮助都将不胜感激,我已经在下面概述了我的代码。如果可能的话,我希望避免两步流程,但是如果有人能为我指出正确的方向,那会有所帮助 项目模型Ruby on rails 轨道4+;Carrierwave+;jQuery文件上传+;嵌套表单多文件问题,ruby-on-rails,ruby-on-rails-4,carrierwave,nested-forms,jquery-fileupload-rails,Ruby On Rails,Ruby On Rails 4,Carrierwave,Nested Forms,Jquery Fileupload Rails,我正在使用的rails应用程序有一个小问题。我正在使用Carrierwave、嵌套表单、简单表单和Jquery文件上传gems 除数据外,大部分工作正常 我有两个模型,一个项目模型和一个附件模型 提交项目表单时,所有文件都会按其应有的方式上载,附件模型中会按应有的方式创建一条记录(每个文件一条记录)。但是对于projects模型,也会为每个文件创建一个记录 我似乎不知道(在一个表单上,一次提交)如何只获取项目的一条记录和附件的多条记录 任何帮助都将不胜感激,我已经在下面概述了我的代码。如果可能的
class Project < ActiveRecord::Base
has_many :attachments, :dependent => :destroy
accepts_nested_attributes_for :attachments, :allow_destroy => true
def generate_token
self.token = loop do
random_token = SecureRandom.urlsafe_base64
break random_token unless Project.where(token: random_token).exists?
end
end
end
class Attachment < ActiveRecord::Base
belongs_to :project, :polymorphic => true
include Rails.application.routes.url_helpers
mount_uploader :file, AttachmentUploader
def to_jq_upload
{
"name" => read_attribute(file),
"url" => file.url,
"size" => file.size,
"delete_url" => attachment_path(:id => id),
"delete_type" => "DELETE"
}
end
end
class项目:销毁
接受:附件的\u嵌套\u属性\u,:允许\u销毁=>true
def生成令牌
self.token=循环do
随机\u令牌=SecureRandom.urlsafe\u base64
中断随机\u令牌,除非Project.where(令牌:随机\u令牌)存在?
结束
结束
结束
附件模型
class Project < ActiveRecord::Base
has_many :attachments, :dependent => :destroy
accepts_nested_attributes_for :attachments, :allow_destroy => true
def generate_token
self.token = loop do
random_token = SecureRandom.urlsafe_base64
break random_token unless Project.where(token: random_token).exists?
end
end
end
class Attachment < ActiveRecord::Base
belongs_to :project, :polymorphic => true
include Rails.application.routes.url_helpers
mount_uploader :file, AttachmentUploader
def to_jq_upload
{
"name" => read_attribute(file),
"url" => file.url,
"size" => file.size,
"delete_url" => attachment_path(:id => id),
"delete_type" => "DELETE"
}
end
end
类附件true
包括Rails.application.routes.url\u帮助程序
mount_uploader:文件,AttachmentUploader
def到jq上传
{
“名称”=>读取属性(文件),
“url”=>file.url,
“size”=>file.size,
“删除url”=>附件路径(:id=>id),
“删除类型”=>“删除”
}
结束
结束
项目控制员
class ProjectsController < ApplicationController
def index
@projects = Project.all
respond_to do |format|
format.html
format.json { render json: @projects }
end
end
def new
@project = Project.new
@project.token = @project.generate_token
@attachments = @project.attachments.build
end
def create
@project = Project.new(project_params)
respond_to do |format|
if @project.save
format.html { redirect_to projects_url, notice: 'Project was successfully created.' }
format.json { render json: @project, status: :created, location: @project }
else
format.html {}
format.json {}
end
end
end
def destroy
@project = Project.find(params[:id])
@project.destroy
respond_to do |format|
format.html { redirect_to projects_url }
format.json { head :no_content }
end
end
private
def project_params
params.require(:project).permit(:name, :token, :number_of_pages, :number_of_copies, :flat_page_size, :trim_page_size, :purchase_order_number, :preferred_delivery_date, :delivery_method, :delivery_instructions, :project_instructions, attachments_attributes: [:id, :attachment, :name, :filename, :file, :project_token, :branch])
end
end
class AttachmentsController < ApplicationController
before_filter :the_project
def index
@attachments = Attachment.where("project_id = ?", the_project)
render :json => @attachments.collect { |p| p.to_jq_upload }.to_json
end
def create
@project = Project.find(params[:project_id])
@attachment = Attachment.new(attachment_params)
if @attachment.save
respond_to do |format|
format.html { render :json => [@attachment.to_jq_upload].to_json, :content_type => 'text/html', :layout => false }
format.json { render :json => {files: [@attachment.to_jq_upload]}.to_json }
end
else
render :json => [{ :error => "custom_failure "}], :status => 304
end
end
def destroy
@attachment = Attachment.find(params[:id])
@attachment.destroy
render :json => true
end
private
def the_project
@project = Project.find(params["project_id"])
end
end
class ProjectsController
附件控制器
class ProjectsController < ApplicationController
def index
@projects = Project.all
respond_to do |format|
format.html
format.json { render json: @projects }
end
end
def new
@project = Project.new
@project.token = @project.generate_token
@attachments = @project.attachments.build
end
def create
@project = Project.new(project_params)
respond_to do |format|
if @project.save
format.html { redirect_to projects_url, notice: 'Project was successfully created.' }
format.json { render json: @project, status: :created, location: @project }
else
format.html {}
format.json {}
end
end
end
def destroy
@project = Project.find(params[:id])
@project.destroy
respond_to do |format|
format.html { redirect_to projects_url }
format.json { head :no_content }
end
end
private
def project_params
params.require(:project).permit(:name, :token, :number_of_pages, :number_of_copies, :flat_page_size, :trim_page_size, :purchase_order_number, :preferred_delivery_date, :delivery_method, :delivery_instructions, :project_instructions, attachments_attributes: [:id, :attachment, :name, :filename, :file, :project_token, :branch])
end
end
class AttachmentsController < ApplicationController
before_filter :the_project
def index
@attachments = Attachment.where("project_id = ?", the_project)
render :json => @attachments.collect { |p| p.to_jq_upload }.to_json
end
def create
@project = Project.find(params[:project_id])
@attachment = Attachment.new(attachment_params)
if @attachment.save
respond_to do |format|
format.html { render :json => [@attachment.to_jq_upload].to_json, :content_type => 'text/html', :layout => false }
format.json { render :json => {files: [@attachment.to_jq_upload]}.to_json }
end
else
render :json => [{ :error => "custom_failure "}], :status => 304
end
end
def destroy
@attachment = Attachment.find(params[:id])
@attachment.destroy
render :json => true
end
private
def the_project
@project = Project.find(params["project_id"])
end
end
class AttachmentsController@attachments.collect{| p | p.to_jq_upload}.to_json
结束
def创建
@project=project.find(参数[:project\u id])
@附件=附件.new(附件参数)
如果@attachment.save
回应待办事项|格式|
format.html{render:json=>[@attachment.to_jq_upload]。to_json,:content_type=>'text/html',:layout=>false}
format.json{render:json=>{files:[@attachment.to_jq_upload]}.to_json}
结束
其他的
render:json=>[{:error=>“custom_failure”}],:status=>304
结束
结束
def销毁
@附件=附件.find(参数[:id])
@附件.销毁
render:json=>true
结束
私有的
def_项目
@project=project.find(参数[“project\u id”])
结束
结束
新项目表单(app/views/projects/New.html.erb)
新项目
{:wrapper_html=>{:class=>'form group'},:input_html=>{:class=>'form control'}},:html=>{:multipart=>true,:id=>“fileupload”,:class=>'horizontal form',:role=>“form”}do | f |%>
“项目名称/说明”:class=>col-lg-12'>
'完成大小(如果与平面页面大小不同)'%>
:文本%>
卡尔加里市区
卡尔加里南部
埃德蒙顿
基洛纳
添加文件。。。
“项目[附件属性][0][文件],:multiple=>true%>
开始上传
取消上传
删除上传
“卡尔加里市中心”%>
@project.token%>
var fileUploadErrors={
maxFileSize:'文件太大',
minFileSize:'文件太小',
acceptFileTypes:“不允许使用文件类型”,
maxNumberOfFiles:“超过最大文件数”,
uploadedBytes:“上载的字节超过文件大小”,
emptyResult:“空文件上载结果”
};
{%for(var i=0,file;file=o.files[i];i++){%}
{%=文件名%}
{%=o.formatFileSize(file.size)%}
{%if(file.error){%}
{%=locale.fileupload.error%}{%=locale.fileupload.errors[file.error]| | file.error%}
{%}如果(o.files.valid&&!i){%}
{%if(!o.options.autoUpload){%}
{%=locale.fileupload.start%}
{% } %}
{%}其他{%}
{% } %}
{%if(!i){%}
{%=locale.fileupload.cancel%}
{% } %}
{% } %}
{%for(var i=0,file;file=o.files[i];i++){%}
{%