ruby on rails 3 - Not able to get url from uploaded image, undefined method `url' error on has_many associated model -
undefined method `url' #<galleryphoto:0x007f80c05a4ba8> 10: <%= @gallery.date %> 11: </p> 12: 13: <%= @gallery.gallery_photos.first.url %> 14: 15: 16: <%= link_to 'edit', edit_gallery_path(@gallery) %>
i attempting create photo album system in rails app albums created , images uploaded via paperclip. unable .url method work on show page display image. way set this:
gallery model (has many gallery_photos)
galleryphotos model(belongs_to gallery)
gallery show:
<p id="notice"><%= notice %></p> <p> <b>gallery name:</b> <%= @gallery.gallery_name %> </p> <p> <b>date:</b> <%= @gallery.date %> </p> <%= @gallery.gallery_photos.first.url %> <%= link_to 'edit', edit_gallery_path(@gallery) %> | <%= link_to 'back', galleries_path %>
gallery model
class gallery < activerecord::base attr_accessible :date, :gallery_name, :gallery_photos_attributes has_many :gallery_photos, :dependent => :destroy accepts_nested_attributes_for :gallery_photos end
gallery_photo model
class galleryphoto < activerecord::base attr_accessible :photo, :caption, :date, :gallery_id belongs_to :gallery has_attached_file :photo,:styles => { :large => "300x300<", :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png" end
gallery controller
def new @gallery = gallery.new @gallery.gallery_photos.build # added end def show @gallery = gallery.find(params[:id]) end def create @gallery = gallery.new(params[:gallery]) respond_to |format| if @gallery.save! format.html { redirect_to @gallery, notice: 'gallery created.' } format.json { render json: @gallery, status: :created, location: @gallery } else format.html { render action: "new" } format.json { render json: @gallery.errors, status: :unprocessable_entity } end end end
the table mysql, , running through vagrant virtual system. inserting on new, , making it. on new inserting data table galleries , gallery_photos. no matter do, cannot url out of it.
from https://github.com/thoughtbot/paperclip, url
method belongs to
has_attached_file :photo
, correct way retrieve url is
@gallery.gallery_photos.first.photo.url
Comments
Post a Comment