Je suis la liste de blogs suivante ci-dessous et je rencontre des problèmes pour que paperclip 2.3.6 fonctionne correctement. Pas tout à fait sûr pourquoi je continue à obtenir cette erreur. J'ai fait un 'rail générez paperclip post_images photo' pour mettre en place un trombone et il semble que tout a bien fonctionné? Tout avis serait grandement apprécié.Problèmes de téléchargement de Paperclip
Blog: http://sleekd.com/general/adding-multiple-images-to-a-rails-model-with-paperclip/
Erreur:
NoMethodError in Admin/postsController#update
undefined method `photo' for #<Post:0xad91a0c>
Rails.root: /home/kyle/code/BlogMe
Application Trace | Framework Trace | Full Trace
app/controllers/admin/posts_controller.rb:26:in `update'
Request
Parameters:
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"yEzBbu3wU1owqoJ5RJo4GvzuwT5RUsz5x6/b+6Zo9ns=",
"post"=>{"user_id"=>"1",
"name"=>"test",
"content"=>"test",
"post_images_attributes"=>{"0"=>{"caption"=>"test",
"photo"=>#<ActionDispatch::Http::UploadedFile:0xaf84d8c @original_filename="8d8933735c9079918df1acb9a8ed0a60.jpeg",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"post[post_images_attributes][0][photo]\"; filename=\"8d8933735c9079918df1acb9a8ed0a60.jpeg\"\r\nContent-Type: image/jpeg\r\n",
@tempfile=#<File:/tmp/RackMultipart20101210-12711-1l8dre>>}}},
"commit"=>"Update Post",
"id"=>"4"}
Code du contrôleur:
def update
if @post.update_attributes(params[:post])
redirect_to admin_posts_path, :notice => "Updated..."
else
@post.post_images.build
render :action => 'edit'
end
end
Afficher le code:
=form_for [:admin,@post], :html => {:multipart => true } do |f|
%p
=f.hidden_field :user_id
=f.text_field :name, {:placeholder => "Enter Blog Title Here" }
%p
=f.text_area :content
%p
=f.fields_for :post_images do |builder|
%p
=builder.label :caption, "Image Caption"
=builder.text_field :caption
%p
=builder.label :photo, "Image File"
=builder.file_field :photo
%p
=f.submit
post.rb
class Post < ActiveRecord::Base
attr_accessible :user_id, :name, :content, :post_images_attributes
has_many :comments
has_many :post_images, :dependent => :destroy
belongs_to :user
validates_presence_of :name
validates_presence_of :content
validates_presence_of :user_id
validates_presence_of :photo
accepts_nested_attributes_for :post_images, :reject_if => lambda { |t| t[:post_image].nil? }
end
post_images.rb
class PostImage < ActiveRecord::Base
belongs_to :post
has_attached_file :photo, :styles => { :small => "150x150", :large => "320x240" }
validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 5.megabytes
end
Quel est votre code d'affichage? Je suppose que vous appelez '<% = builder.label: la photo," Image File "%><% = builder.file_field: photo%>' est incorrecte, et vous utilisez 'f.file_field' au lieu de' builder .form_field' – iain
J'ai posté ma vue. –