我是铁杆新手。 还有网络开发。
很抱歉,如果有些问题看起来很愚蠢。
尝试按照此屏幕投射 - http://emersonlackey.com/screencasts/rails-3-with-paperclip.mov
但停止了问题 - 当我尝试上传图片时,我收到以下错误:
PostsController#update中的ActiveRecord :: UnknownAttributeError 未知属性:图像
altough post_controller.rb似乎没问题(多次检查 - 它与https://github.com/Emerson/Multiple-File-Uploads-with-Paperclip-and-Rails-3相同):
当然尝试了googlin,但没有找到任何东西。
是否有人通过本教程并遇到此问题?
答案 0 :(得分:2)
问题已修复,_form代码不正确!
我不得不改变:
<%= f.fields_for :assets do |asset| %>
到
<%= f.fields_for :assets do |asset_fields| %>
和
<%= asset.file_field :image %>
到
<%= asset_fields.file_field :asset %>
它有效。
原因很愚蠢,我只是没看到截屏直到最后,因为我停在中间 - 当问题出现时,我全神贯注地搜索解决方案。
初学者错了!
答案 1 :(得分:0)
帖子模特:
class Post < ActiveRecord::Base
attr_accessible :title, :content, :assets_attributes
has_many :assets
accepts_nested_attributes_for :assets, :allow_destroy => true
end
资产模型:
class Asset < ActiveRecord::Base
belongs_to :post
has_attached_file :asset, :styles => { :large => "640x480", :medium=>"300x300>",
:thumb => "100x100>" }
end
邮政控制人:
class PostsController < ApplicationController
def index
@posts = Post.all
end
def show
@post = Post.find(params[:id])
end
def new
@post = Post.new
5.times { @post.assets.build }
end
def create
@post = Post.new(params[:post])
if @post.save
redirect_to @post, :notice => "Successfully created post."
else
render :action => 'new'
end
end
def edit
@post = Post.find(params[:id])
5.times { @post.assets.build }
end
def update
@post = Post.find(params[:id])
if @post.update_attributes(params[:post])
redirect_to @post, :notice => "Successfully updated post."
else
render :action => 'edit'
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
redirect_to posts_url, :notice => "Successfully destroyed post."
end
end
_form:
<%= form_for @post, :html => { :multipart => true } do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.label :content %><br />
<%= f.text_area :content %>
</p>
<%= f.fields_for :assets do |asset| %>
<% if asset.object.new_record? %>
<%= asset.file_field :image %>
<% end %>
<% end %>
<p><%= f.submit %></p>
<% end %>
模型索引:
<% title "Posts" %>
<p><%= link_to "New Post", new_post_path %></p>
<hr />
<% for post in @posts %>
<div class="post">
<h2><%= link_to post.title, post%></h2>
<p class="content">
<%= post.content.html_safe %>
<br /><br />
<%= link_to "Edit", edit_post_path(post) %> | <%= link_to "Destroy", post,
:confirm => 'Are you sure?', :method => :delete %>
</p>
</div>
<% end %>
错误:
Started PUT "/posts/1" for 127.0.0.1 at Tue Sep 20 11:00:52 +0300 2011
Processing by PostsController#update as HTML
Parameters: {"commit"=>"Update Post", "post"=>{"title"=>"AAAAA", "content"=>"T
he enormous success of DropBox clearly shows that there's a huge need for simple
and fast file sharing.\r\n\r\nOur app will have the following features:\r\n\r\n
simple user authentication\r\n upload files and save them in Amazon S3\r\
n create folders and organize\r\n share folders with other users\r\n\r\nTh
roughout the tutorial, I will point out different ways that we can improve our a
pp. Along the way, we'll review a variety of concepts, including Rails scaffoldi
ng and AJAX.", "assets_attributes"=>{"0"=>{"image"=>#<ActionDispatch::Http::Uplo
adedFile:0x436fda0 @tempfile=#<File:C:/DOCUME~1/emve/LOCALS~1/Temp/RackMultipart
20110920-8900-zgz1ej-0>, @headers="Content-Disposition: form-data; name=\"post[a
ssets_attributes][0][image]\"; filename=\"02.jpg\"\r\nContent-Type: image/jpeg\r
\n", @content_type="image/jpeg", @original_filename="02.jpg">}}}, "authenticity_
token"=>"WHsbBak0O2xYBFe/h82+4/5aV2VPzHDdXcgb4QYmC4A=", "utf8"=>"Ō£ō", "id"=>"1"
}
←[1m←[35mPost Load (0.0ms)←[0m SELECT "posts".* FROM "posts" WHERE "posts"."i
d" = ? LIMIT 1 [["id", "1"]]
Completed 500 Internal Server Error in 63ms
ActiveRecord::UnknownAttributeError (unknown attribute: image):
app/controllers/posts_controller.rb:32:in `update'
Rendered C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/actionpack-3.1.0/lib
/action_dispatch/middleware/templates/rescues/_trace.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/actionpack-3.1.0/lib
/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/actionpack-3.1.0/lib
/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/lay
out (46.9ms)