Here is a series of queries that your use case goes through:
First, the form passes some data to your Album#create action.
After the Album#create request completes successfully, the request is redirected to the Albums#show action.
In the Albums#show action, the controller creates instances of @user and @album with your current user and new album, and then creates a new Photo object in the Album photos collection.
Then the action saves the Album record again. Since nothing has changed, it actually does nothing. But it does not do anything successfully, so it redirects the request to album_photo_path , and this is what happens in a pear-shaped form . This route, as defined, results in a Photo#show action in the context of the parent photo album. The route expects two arguments: an album and a photo. As called in the code, it should not handle the routing error exception. (Believe me, I tried.)
Instead of hanging on minutae like this, though, I'm going to make a blind recommendation instead!
It looks like you did not intend to perform your Album#show action to save the album and redirect to Photo#show via the wrong route. I think this is the source of all three of your problems. And ultimately, I think your AlbumsController show action should look like this:
def show @user = User.find(params[:user_id]) @album = @user.albums.find(params[:id]) end
Hope this helps!
Edited to post Edmund's comment:
Absolutely, you can configure the Album#show action so that users can add photos. This will be due to the two changes that I propose above.
In your controller, you will create a new photo in the @album Photos collection, as before:
def show @user = User.find(params[:user_id]) @album = @user.albums.find(params[:id]) @photo = @album.photos.build end
Then in the Album#show template (for example, app/views/albums/show.html.erb ) you must enable the form to submit a new photo:
<%= form_for [@user, @album, @photo] do |f| %> <%= f.text_field :caption %> <%= f.file_field :image %> <%- end %>
This form should present its contents in user_album_photos_path , which, as you can see, does not exist yet. So the final change will be to embed Photos as a resource in routes.rb :
resources :users do resources :albums do resources :photos end end
This will provide you with the route necessary to use the form. Now you just need to add the Photo#create action to process the form submission, and you will go racing!