【问题标题】:Rails on Windows File Upload without gems没有 gems 的 Windows 文件上传 Rails
【发布时间】:2014-08-20 08:46:02
【问题描述】:

我已经挣扎了十多个小时,试图让文件上传自己工作,但我认为我自己无法做到。几个小时后,我无法让 Paperclip 或 Carrierwave 工作,可能是因为我使用的是 windows,至少在短期内是这样。我又花了几个小时试图手动安装它们。然后我花了几个小时试图在没有 gems 的情况下使文件上传工作。我已经解决了每个错误,我认为我非常接近。我要发布我所有的信息。跟踪和错误使代码变得混乱,但我怀疑只剩下一两个问题。我正在博客脚手架上工作。我有文章类,评论类属于文章,试图将文件附加到文章。

我在尝试上传时出现的当前错误。

ActiveRecord::AssociationTypeMismatch in ArticlesController#create
Picture(#46001052) expected, got ActionDispatch::Http::UploadedFile(#29170272)

这是 article_controller.rb 中的错误行。

@article = Article.new(article_params)

我觉得奇怪的是它需要 #46001052 但收到一个带有 #29170272 的文件

这是我的文章_controller.rb。我怀疑这是问题所在。我认为没有使用 def 上传,它只是跟踪和错误的遗留物。当我删除它时,我得到了同样的错误。

class ArticlesController < ApplicationController

http_basic_authenticate_with name: "Goose", password: "123456",
except: [:index, :show, :new, :create]

def new
    @article = Article.new
end

def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render 'new'
end
end

def show
@article = Article.find(params[:id])
end

def index
@articles = Article.all
end

def edit
@article = Article.find(params[:id])
end

def update
@article = Article.find(params[:id])

if @article.update(article_params)
redirect_to @article
else
render 'edit'
end
end

def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_path
end

def upload
uploaded_io = params[:article][:picture]
File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'wb') do |file|
file.write(uploaded_io.read)
end
end

private
def article_params
params.require(:article).permit(:title, :text, :picture)
end

end

为了以防万一,我还用一些类似的东西制作了一个 pictures_controller.rb。如有要求,将发布。

这是我的文章.rb 模型

class Article < ActiveRecord::Base
has_many :comments, dependent: :destroy
has_one :picture, dependent: :destroy
validates :title, presence: true,
                length: { minimum: 5 }

  end

这是我的图片.rb 模型

class Picture < ActiveRecord::Base
belongs_to :article
end

这是我的_form。我很有信心这是对的。

<%= form_for @article, :html => { :multipart => true } do |f| %>
<% if @article.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@article.errors.count, "error") %> prohibited
  this article from being saved:</h2>
<ul>
<% @article.errors.full_messages.each do |msg| %>
  <li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.text_field :title , :size => "60X10" %>
</p>

<p>
<%= f.text_area :text , :size => "60x10" %>
</p>

<p>
<%= f.file_field :picture %>
</p>

<p>
<%= f.submit "Talk" %>
</p>
<% end %>

这是我的 routes.rb。为了安全起见,我从 :cmets 拆分了 :uploads,但无论哪种方式我都会遇到相同的错误。

Blog::Application.routes.draw do

resources :articles do
resources :comments
end

resources :articles do
resources :uploads
end
root 'welcome#index'
end

Rails.application.routes.draw do
get 'welcome/index'

我在某处读到向我的表中添加一列,所以我添加了这个名为 add_new_column_to_my_table.rb 的迁移

class AddNewColumnToMyTable < ActiveRecord::Migration
def change
self.up
add_column :articles, :new_column, :picture
end

end
end

我还做了一个 create_pictures.rb 迁移。

class CreatePictures < ActiveRecord::Migration
def change
create_table :pictures do |t|
  t.string :image
  t.references :article, index: true
end
end
end

我想出了如何将文件保存到目录,甚至给该文件一个唯一的文件名,即创建时的时间戳,但我还没有想出如何将该文件链接到它的匹配文章。只是在我创建文件时将时间保存为变量就可以了,但控制器显然不是这样做的好地方。

我已经全神贯注,很明显我需要帮助。我希望您在解决这个问题时比我遇到的更轻松。

更新:根据要求,这是我的 schema.rb 代码,看起来确实很可疑。

ActiveRecord::Schema.define(version: 20140630023622) do
create_table "comments", force: true do |t|
t.string   "commenter"
t.text     "body"
t.integer  "article_id"
t.datetime "created_at"
t.datetime "updated_at"
end

add_index "comments", ["article_id"], name: "index_comments_on_article_id"

create_table "data_files", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "pictures", force: true do |t|
t.string "image"
end

create_table "uploads", force: true do |t|
t.string   "timestamp"
t.text     "fileid"
t.integer  "article_id"
t.datetime "created_at"
t.datetime "updated_at"
end

add_index "uploads", ["article_id"], name: "index_uploads_on_article_id"

end

【问题讨论】:

  • 这是一个很好的问题,是我遇到的问题,你能解决吗?
  • @BenSmith 这是很久以前我第一次开始编程的时候,但没有。我最终发现 Rails 社区非常以 Mac 为中心,因此我转向其他语言。

标签: ruby-on-rails ruby windows file-upload


【解决方案1】:

在运行 AddNewColumnToMyTable 迁移之前,您是否遇到过这个问题?如果没有,我看到两个问题。首先,您尝试编写的迁移应如下所示:

class AddNewColumnToMyTable < ActiveRecord::Migration
  def change
    add_column :articles, :picture, :string
  end
end

这将告诉数据库将名为图片的字符串列添加到文章架构中。第二个问题是您正在将“图片”添加到已经与名为图片的模型有关系的模型中。这意味着迁移需要为图片使用不同的名称:

class AddNewColumnToMyTable < ActiveRecord::Migration
  def change
    add_column :articles, :picture_id, :string
  end
end

如果这不起作用,请发布您的 schema.rb 文件。

【讨论】:

  • 不走运,同样的错误。你想让我用第二个代码块创建一个名为 AddNewColumnToMyTable_controller.rb 的控制器吗,因为我就是这么做的。
  • 这是您的整个 schema.rb 文件吗?那里甚至没有文章表。
  • 省略了这个。 //ActiveRecord::Schema.define(version: 20140630023622) 做
  • 当您运行 AddNewColumnToMyTable 迁移时,您应该收到一个错误,即没有名为 article 的表。创建 AddNewColumnToMyTable 迁移后,您是否在终端中运行了 rake db:migrate?
  • 好吧无论如何你需要创建一个文章表
【解决方案2】:

Paperclip

我们总是使用 Paperclip 之类的 - 所以如果可以的话,我很乐意提供解决您在 Windows 上的“兼容性”问题的方法

默认情况下,Paperclip 可以在任何平台上运行,因为它不依赖任何外部依赖项;您的问题可能与 Paperclip 与 ImageMagick 的集成有关(要正确处理非常棘手):

--

ImageMagick

ImageMagick 是允许 Paperclip 更改图像的不同后端选项的处理器。它允许在您上传图片时执行调整大小、裁剪和其他图片更改过程。

ImageMagick 与 Windows 一起工作非常棘手。我们发现最好的方法是使用以下版本:

ImageMagick 6.8.7 Q16 (32 bit)

有一个excellent resource here 可以下载以前版本的 ImageMagick

--

考虑到您已安装并迁移了 Paperclip gem - 您应该可以使用 Paperclip 上传;不管系统的其他部分发生了什么

【讨论】:

  • 我什至在安装 ImageMagick 之前就遇到了终端识别回形针的问题,我确实按照指南引导我了解回形针和 imagemagick 的兼容性。
  • 好的,感谢您的评论!你有什么错误可以参考我吗?我想为您修复回形针 - 这比使用自制上传系统要好得多
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-06
  • 1970-01-01
  • 2019-06-06
  • 2012-11-05
  • 2011-10-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多