bigpotato
bigpotato

Reputation: 27547

Rails: pictures not uploading?

I'm working on my first rails project and am trying to use a file_field to upload photos that belongs_to :album. The form appears on my albums/show.html.erb page, since I wanted to show the album and have the user be able to upload pics from one place. However, when I press submit on the form, it doesn't seem to upload.

here is my photos/_form.html.erb

<%= form_for(@photo, :html => { :multipart => true }, :url => { :action => 'create'} ) do |f| %>

<%= f.label :avatar %>
<%= f.file_field :avatar %>

<%= f.submit %>


<% end %>

this is my albums/show.html.erb page. I added the if/else statement just to test if the @album instance was receiving the picture I uploaded, but it always comes back with "no"

<% if @album.photos.any? %>
yes
<% else %>
no
<% end %>


<div>
    <%= render 'photos/form' %>
</div>

photos controller (i'm really confused as to what to set the instance variables in this)

class PhotosController < ApplicationController


def create
  @album = Album.find(params[:user_id][:album_id])
  @photo = @album.build(params[:photo])
  respond_to do |format|
    if @album.save
      format.html { redirect_to @album, notice: 'Album was successfully created.' }
      format.json { render json: @album, status: :created, location: @album}
    else
      format.html { render action: "new" }
      format.json { render json: @album.errors, status: :unprocessable_entity }
    end
end
end

album model

class Album < ActiveRecord::Base
  attr_accessible :avatar, :name, :description
  has_many :user_albums
  has_many :users, :through => :user_albums
  has_many :photos
end

photo model

class Photo < ActiveRecord::Base
  belongs_to :album 
end

Let me know if you need any other files

Upvotes: 0

Views: 44

Answers (1)

kiddorails
kiddorails

Reputation: 13014

You will need,

@album.photos.build(params[:photo])

Also, I assume that your uploading mechanism is correct. :)

Good luck

Upvotes: 1

Related Questions