undefined method in my view

时间:2015-08-07 01:52:28

标签: ruby-on-rails ruby carrierwave erb

I'm using carrierwave (https://www.youtube.com/watch?v=GnVIRa7Jr6E) to for someone to get and post an image. But, in my show.html.erb file, it says that I have an unidentified method:

<%= image_tag @article.image_tag.to_s %>

"image_tag" is my undefined method

and in my controller:

private
      def article_params
          params.require(:article).permit(:title, :text, :image_tag)
      end

if you need it, I have a painting model (painting.rb):

class Painting < ActiveRecord::Base
    attr_accessable :gallary_id, :name, :image
    belongs_to :gallary
    mound_uploader :image, ImageUploader
end

1 个答案:

答案 0 :(得分:2)

I think you should use:

<%= image_tag @article.image %> or maybe <%= image_tag @article.image.url %>

On your code on Github there's something strange. Your Article class do not have an image or an uploader for something. And the painting class is not in a relationship with Article either.

If you want to try something to test carrierwave, I suggest you add a migration for the article let's say a miniature. And then mount the uploader on this field. Then call it in your view.

The carrierwave documentation should be useful: https://github.com/carrierwaveuploader/carrierwave

EDIT

Here is what you can do (simple experiment):

First let's add add a new column for the article.

rails g migration addImageToArticle image:string

Run the migration

rake db:migrate

Mount the uploader on the Article class:

class Article < ActiveRecord::Base
    has_many :comments, dependent: :destroy
    validates :title, presence: true,
                      length: { minimum: 5 }

    mount_uploader :image, ImageUploader
end

Update your ArticlesController

params.require(:article).permit(:title, :text, :image_tag, :image)

Add the field in your form:

<%= f.file_field :image %>

Then use it in the views (articles/show)

<%= image_tag @article.image.url %> or <%= image_tag @article.image_url %> (as the carrierwave doc suggests)

Now it should work!

If your new to Rails, I would suggest this video too: http://railscasts.com/episodes/253-carrierwave-file-uploads