2011-09-19 82 views
0

我是新來的鐵軌。 對網頁開發。多個文件上傳與回形針錯誤

對不起,如果有些問題可能看起來愚蠢。

試圖按照此屏幕鑄 - http://emersonlackey.com/screencasts/rails-3-with-paperclip.mov

但停在了問題 - 當我嘗試上傳圖片我得到以下錯誤:

的ActiveRecord :: UnknownAttributeError在PostsController#更新 未知的屬性:圖像

altough post_controller.rb似乎確定(檢查很多次 - 這是一樣的https://github.com/Emerson/Multiple-File-Uploads-with-Paperclip-and-Rails-3):

嘗試googlin當然,但沒有找到任何東西。

有沒有人槽本教程,並有這個問題?

+0

顯示一些代碼,比如你正在剪報的東西的模型,以及導致錯誤的動作 - 否則我們只是猜測。該錯誤表示模型可能未正確註釋,或者控制器未使用正確的屬性名稱。 –

+0

請包括您的觀點:表單代碼。 –

回答

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) 
2

問題固定的,_form代碼,是不正確的!

我不得不改變:

<%= f.fields_for :assets do |asset| %> 

<%= f.fields_for :assets do |asset_fields| %> 

<%= asset.file_field :image %> 

<%= asset_fields.file_field :asset %> 

它的工作。

原因很愚蠢,我只是沒有看到屏幕截圖直到最後,因爲我停在了中間 - 問題出現時,我把所有的注意力都花在搜索解決方案上。

初學者錯!

+0

謝謝!我遇到了同樣的問題:-) – buk