2014-10-03 66 views
1

我從曲別針將我的應用程序基於carrierwave的GitHub上的文檔上carrierwave:https://github.com/carrierwaveuploader/carrierwave和建議,從這個博客:http://bessey.io/blog/2013/04/07/migrating-from-paperclip-to-carrierwave/將回形針轉換爲Carrierwave |未定義的方法'存在?'

但是我得到一個未定義的方法錯誤'存在」在文章#show中。

這裏是我的_form.html.erb代碼

<%= form_for(@article, html: {multipart: true}) do |f| %> 
     . 
     . 
     . 
     <p> 
      <% if @article.image.exists? %> 
       <%= image_tag @article.image.url %><br /> 
      <% end %> 
      <%= f.label :image, "Attach a New Image" %><br /> 
      <%= f.file_field :image %> 
     </p> 
     <p> 
      <%= f.submit %> 
     </p> 
     <% end %> 

下面是文章#顯示

def show 
     @article = Article.find(params[:id]) 
    end 

的文章型號

class Article < ActiveRecord::Base 
    mount_uploader :image, ImageUploader, :mount_on => :image 
    default_scope -> { order('created_at DESC') } 
    has_many :comments 
    has_many :taggings 
    has_many :tags, through: :taggings 
    # has_attached_file :image 
    # validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/gif", "image/png"] 

    def total_pages 
     @articles = Article.all 
    end 

    def tag_list 
     self.tags.collect do |tag| 
      tag.name 
     end.join(", ") 
    end 

    def tag_list=(tags_string) 
     tag_names = tags_string.split(",").collect{|s| s.strip.downcase}.uniq 
     new_or_found_tags = tag_names.collect { |name| Tag.find_or_create_by(name: name) } 
     self.tags = new_or_found_tags 
    end 
end 

讓我知道你是否需要看別的東西。我仍然在學習rails,我不知道我需要在哪裏調試這個特定的錯誤。

回答

1

要查看圖像的存在,你可以只使用

<% if @article.image_url %> 
    <%= image_tag @article.image.url %><br /> 
<% end %> 

關於你的應用程序設置載波欲瞭解更多信息檢查railscasts

+0

謝謝這個小插曲!這工作完美。感謝您的railscast參考。 – latazzajones 2014-10-03 11:48:42

+0

歡迎:D – 2014-10-03 15:25:55

相關問題