【发布时间】: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