ruby on rails - Why are Paperclip images coming up as missing.png? -
i know there couple of questions out there on stackoverflow, either went right on head or didn't seem relevant own question.
i working on jumpstartlab's blogger 2 project tutorial , have reached i4 after completing beforehand. tutorial told install paperclip gem, add:
class addpaperclipfieldstoarticle < activerecord::migration def change add_column :articles, :image_file_name, :string add_column :articles, :image_content_type, :string add_column :articles, :image_file_size, :integer add_column :articles, :image_updated_at, :datetime end end
to new migration database add paperclip fields article (and rake this).
i added code:
has_attached_file :image validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png"]
to apps/models/article.rb, added :image app/helpers/articles_helper.rb
here _form.html.erb
<%= form_for(@article, html: {multipart: true}) |f| %> <ul> <% @article.errors.full_messages.each |error| %> <li><%= error %></li> <% end %> </ul> <p> <%= f.label :title %><br /> <%= f.text_field :title %> </p> <p> <%= f.label :body %><br /> <%= f.text_area :body %> </p> <p> <%= f.label :image, "attach image" %><br /> <%= f.file_field :image %> </p> <p> <%= f.submit %> </p> <p> <%= f.label :tag_list %><br /> <%= f.text_field :tag_list %> </p> <% end %>
why image add changing missing.png? have tried multiple images, changed paperclip older version, output has been same.
thanks in advance.
edit:
view code show.html.erb
<h1><%= @article.title %></h1> <p><%= image_tag(@article.image.url) %></p> <p><%= @article.body %></p> <h3>comments (<%= @article.comments.size %>)</h3> <%= render partial: 'articles/comment', collection: @article.comments %> <%= render partial: 'comments/form' %> <%= link_to "<< articles' list", articles_path %> <%= link_to "delete", article_path(@article), method: :delete, data: {confirm: "really delete article?"} %> <%= link_to "edit", edit_article_path(@article) %> <p> tags: <% @article.tags.each |tag| %> <%= link_to tag.name, tag_path(tag) %> <% end %> </p>
articles_controller code
class articlescontroller < applicationcontroller include articleshelper def index @articles = article.all end def show @article = article.find(params[:id]) @comment = comment.new @comment.article_id = @article.id end def new @article = article.new end def create @article = article.new(article_params) @article.save flash.notice = "article '#{@article.title}' created!" redirect_to article_path(@article) end def destroy @article = article.find(params[:id]) @article.destroy flash.notice = "article '#{@article.title}' deleted!" redirect_to action: 'index' end def edit @article = article.find(params[:id]) end def update @article = article.find(params[:id]) @article.update(article_params) flash.notice = "article '#{@article.title}' updated!" redirect_to article_path(@article) end end
why paperclip images coming missing.png?
this literally describes problem. when article's :image
nil
, no matter try, show default missing.png
because case handled paperclip
itself.
however, can use default_url option set custom default image when article's image missing.
alternatively, can use conditional this:
if @article.image_file_name.present? # makes sure article has image # show article image else # show custom default image here end
update
basically, image uploading not working. make sure have in controller:
def article_params params.require(:article).permit(:image) end
i suspect, there validation error when create article. make sure, change create
method this:
def create @article = article.create!(article_params) flash.notice = "article '#{@article.title}' created!" redirect_to article_path(@article) end
using create!
instead of new
throw exception if there error while creating article , able know exact problem.
try , let me know! if error when using create!
while creating article, update question information. image content type validation error. if that's case, add custom validation image , content_type , make sure works. but, see!
Comments
Post a Comment