0
votes

I can't seem to get the hang of routes when it comes to nested parameters and the controllers for them. Later edit - routes seemed fine, but parent_id is not saved

In my routes I have

   resources :galleries do
     resources :album
   end

Then show in controller

def show
  @albums = Album.find(params[:id])
  @photos = @albums.photos.all

end

def index
  @albums = Album.includes(:photos).all


end

def new
   @gallery = Gallery.find(params[:gallery_id])
   @album = @gallery.albums.build
   @photos = @album.photos.build
end

def create
   @gallery = Gallery.find(params[:gallery_id])
   @album = @gallery.albums.new(album_params)

   respond_to do |format|
     if @album.save
       params[:photos]['image'].each do |a|
          @photo = @album.photos.create!(:image => a, :album_id => @album.id)
       end
       format.html { redirect_to gallery_path(@gallery.id), notice: 'Post was successfully created.' }

Models are like this

class Album < ApplicationRecord
  belongs_to :gallery
  has_many :photos
  accepts_nested_attributes_for :photos
end

class Gallery < ApplicationRecord
  has_many :albums
  accepts_nested_attributes_for :albums
end

In my form for galleries, I see fine all galleries column values. Now I am trying to display a list of its albums like this for starters, but nothing shows up:

<% @gallery.albums.each do |album| %>   
  <%= album.name %>
  <%= link_to "Destroy", album, method: :delete %>
<% end %>

It turns out that the parent_id of the gallery is not being saved when I create a new album

I call the album creation like this

<%= link_to 'New Album', new_gallery_album_path(@galleries.id) %>

which leads to this url http://localhost:3000/galleries/1/albums

I am not exactly sure what is missing from the create definition in order for the gallery_id to be saved when I save the album

Later edit

Console before applying @Esther's suggestion

Gallery Load (1.0ms) SELECT "galleries".* FROM "galleries" WHERE "galleries"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]] (0.0ms) BEGIN SQL (0.0ms) INSERT INTO "albums" ("band", "gallery_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["ban d", "test"], ["gallery_id", 1], ["created_at", "2017-09-18 21:09:59.805397"], ["updated_at", "2017-09-18 21:09:59.805397"]]
(0.0ms) COMMIT (0.0ms) BEGIN

And after modifying the controller to use @gallery.albums.create(album_params) instead of @gallery.albums.create

Gallery Load (0.0ms) SELECT "galleries".* FROM "galleries" WHERE "galleries"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]] (0.0ms) BEGIN SQL (1.0ms) INSERT INTO "albums" ("band", "gallery_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["ban d", "new"], ["gallery_id", 1], ["created_at", "2017-09-19 18:28:41.093138"], ["updated_at", "2017-09-19 18:28:41.093138"]] (0.0ms) COMMIT (0.0ms) BEGIN (1.0ms) COMMIT (0.0ms) BEGIN

2

2 Answers

1
votes

By "parent_id is not saved", do you mean gallery_id is not being saved to album?

If yes, my first thought is I think you should change this line of code in your album create method from @album = @gallery.albums.new(album_params) to @album = @gallery.albums.create(album_params). This immediately creates the album with the existing params in album_params which I hope includes the gallery_id in it before you proceed with adding the photos to the album.

Secondly, can you please update your question with the logs from the console so that we can confirm what's in the album_params.

1
votes

Looks to me like there aren't any album records associated with this gallery. You can confirm by printing the count out or with a debug statement.

# Example:
There are <%= @albums.count %> albums associated with this gallery.

# For debugging only:
<%= debug @albums %>

You can also see this in the console.

@gallery.albums.count 
@gallery.albums.any?

And if you think something is awry you can check for records in the database directly (run rails db to connect to your database using the same credentials) and then you can execute SQL:

SELECT COUNT(*) FROM albums WHERE gallery_id = 1;