0

I'm new to rails. And to web development at all.

Sorry if some questions might seem dumb.

Trying to follow this screen cast - http://emersonlackey.com/screencasts/rails-3-with-paperclip.mov

But stopped at the problem - when i try to upload an image i get the following error :

ActiveRecord::UnknownAttributeError in PostsController#update Unknown attribute: image

altough post_controller.rb seems ok (checked many times - it is the same as https://github.com/Emerson/Multiple-File-Uploads-with-Paperclip-and-Rails-3) :

Tried googlin of course, but didn't find anything.

Has anyone been trough this tutorial and had this problem ?

2
  • Show some code, like the model you're paperclipping things to, and the action causing the error--otherwise we're just guessing. The error indicates the model probably isn't correctly annotated, or the controller isn't using the right property name. Commented Sep 19, 2011 at 16:43
  • Please include your view:form code as well. Commented Sep 19, 2011 at 16:45

2 Answers 2

2

Problem fixed, the _form code, was incorrect!

I had to change:

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

to

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

and

<%= asset.file_field :image %>

to

<%= asset_fields.file_field :asset %>

And it worked.

The reasons was quite silly, i just didn't the watch the screencast till the end, because I stopped at the middle - when the problem showed-up, and spent my whole attention googling for the solution.

Beginners mistake!

Sign up to request clarification or add additional context in comments.

Comments

0

Posts model:

class Post < ActiveRecord::Base
  attr_accessible :title, :content, :assets_attributes
  has_many :assets
  accepts_nested_attributes_for :assets, :allow_destroy => true
end

Assets model:

class Asset < ActiveRecord::Base
  belongs_to :post
  has_attached_file :asset, :styles => { :large => "640x480", :medium=>"300x300>", 
  :thumb => "100x100>" }
end

Post controller:

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 %>

Model index:

<% 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 %>

The error:

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.