0

In my web app each album has many photos and each photo belongs to an album. I have already done the associations along with the new and create actions for photos and I am not getting an error, however, when I go to the albums show actions the newly created photos are not appearing.

View:

        <% @photos.each do |x| %>
            <div class="new-photo">
                <%= image_tag x.image, :style => "height:200px; width:200px; border-radius:5px;" %>
            </div>
        <% end %>

Albums controller:

def show
    @album = current_user.albums.find(params[:id])
    @photos = @album.photos
    @interests = current_user.interests
end

Photos controller:

def new
    @photo = current_user.photos.new
end

def create
    @photo = current_user.photos.new(photo_params)
    if @photo.save
        redirect_to '/albums'
    else
        render '/photos/new'
    end
end

private
def photo_params
    params.require(:photo).permit(:image)
end

Photo Model:

class Photo < ActiveRecord::Base
belongs_to :album
belongs_to :user

end

Album model:

class Album < ActiveRecord::Base
belongs_to :user
has_many :photos

end

nums
  • 61
  • 1
  • 8
  • use `build` instead of `new` in your create action. [this](http://stackoverflow.com/questions/4954313/build-vs-new-in-rails-3) might help you. – The F Jun 28 '16 at 18:07
  • Still got the same result – nums Jun 28 '16 at 18:13
  • could you past your model code? do you have any validations? – Sean Jun 28 '16 at 18:26
  • How do you upload images? params.require(:photo).permit(:image)...what would be values for image param? – Mirza Memic Jun 28 '16 at 18:27
  • You've double checked the database to be sure your photos are saving and saving to the correct album? – Luke Jun 28 '16 at 19:10
  • The value for (:image) is text @MirzaMemic – nums Jun 29 '16 at 16:26
  • when you run this inside console @album = current_user.albums.find(params[:id]) and then later album.photos...assuming that you have current user what do you get as a result? It looks like your db is empty or not saved...also sedn a vlaues of one text image if possible, – Mirza Memic Jun 29 '16 at 17:24
  • When I checked the database the photos were saving with valid user__id but the album_id was always nil @MirzaMemic – nums Jun 30 '16 at 00:12
  • When I checked the database the photos were saving with valid user__id but the album_id was always nil @Luke – nums Jun 30 '16 at 00:12

1 Answers1

0

So it seems that image is created without an album id. Inside new action then album_id should be assigned in order to display photos later with @photos = @album.photos

this line

params.require(:photo).permit(:image)

should include

params.require(:photo).permit(:image, :album_id)
Mirza Memic
  • 862
  • 5
  • 9