Not able to get url from uploaded image, undefined method `url' error on has_many associated model

undefined method `url' for #<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 am attempting to create a photo album system in a rails app where albums are created and images are uploaded to it via paperclip. I am unable to get the .url method to work on my show page to display the image. The way it is set up is like 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 this
  end

  def show
    @gallery = Gallery.find(params[:id])
  end

  def create
    @gallery = Gallery.new(params[:gallery])

    respond_to do |format|
      if @gallery.save!
        format.html { redirect_to @gallery, notice: 'Gallery was successfully 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 is mysql, and I am running this through a vagrant virtual system. It is inserting on new, and it is making it. On new it is inserting data into the table for the galleries and for gallery_photos. No matter what I do, I cannot get a url out of it.

Answers


From https://github.com/thoughtbot/paperclip, the url method belongs to has_attached_file :photo, so the correct way to retrieve the url is

@gallery.gallery_photos.first.photo.url

Need Your Help

Build DLL for JNI using Eclipse

java eclipse dll jni cygwin

I want to build a JNI DLL using Eclipse and Cygwin. I know there are a lot of examples out there, but non of them work for me. I've spent 2 days on this and I still can't get Eclipse to build a f...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.