zyang91
zyang91

Reputation: 83

Undefined method 'album_url' when deleting a photo and redirecting

I have user, album, and photo models set up as the following:

user.rb

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :albumusers
  has_many :albums, through: :albumusers, :foreign_key => :album_id

end

album.rb

class Album < ActiveRecord::Base
    validates :name, presence: true

    has_many :photos

    has_many :albumusers
    has_many :users, through: :albumusers, :foreign_key => :user_id
end

photo.rb

class Photo < ActiveRecord::Base
    belongs_to :album

    mount_uploader :image, PhotoUploader

    before_create :default_title

    def default_title
        self.title ||= File.basename(image.filename, '.*').titleize if image
    end
end

albumuser.rb

class Albumuser < ActiveRecord::Base
    belongs_to :user
    belongs_to :album
end

routes.rb

resources :users do
    resources :albums do
        resources :photos
    end
end

photo_controller.rb

  def destroy
    @photo = Photo.find(params[:id])
    @photo.destroy
    flash[:notice] = "Successfully deleted photo"
    redirect_to @photo.album # ERROR is happening here
  end

When I try to delete a photo, I get error when trying to redirect back to the album show view.

Upvotes: 0

Views: 91

Answers (1)

Martin M
Martin M

Reputation: 8658

The reason is in your routes:
your routes give you

    user_album_photos GET    /users/:user_id/albums/:album_id/photos(.:format)          photos#index
                      POST   /users/:user_id/albums/:album_id/photos(.:format)          photos#create
 new_user_album_photo GET    /users/:user_id/albums/:album_id/photos/new(.:format)      photos#new
edit_user_album_photo GET    /users/:user_id/albums/:album_id/photos/:id/edit(.:format) photos#edit
     user_album_photo GET    /users/:user_id/albums/:album_id/photos/:id(.:format)      photos#show
                      PATCH  /users/:user_id/albums/:album_id/photos/:id(.:format)      photos#update
                      PUT    /users/:user_id/albums/:album_id/photos/:id(.:format)      photos#update
                      DELETE /users/:user_id/albums/:album_id/photos/:id(.:format)      photos#destroy
          user_albums GET    /users/:user_id/albums(.:format)                           albums#index
                      POST   /users/:user_id/albums(.:format)                           albums#create
       new_user_album GET    /users/:user_id/albums/new(.:format)                       albums#new
      edit_user_album GET    /users/:user_id/albums/:id/edit(.:format)                  albums#edit
           user_album GET    /users/:user_id/albums/:id(.:format)                       albums#show
                      PATCH  /users/:user_id/albums/:id(.:format)                       albums#update
                      PUT    /users/:user_id/albums/:id(.:format)                       albums#update
                      DELETE /users/:user_id/albums/:id(.:format)                       albums#destroy
                users GET    /users(.:format)                                           users#index
                      POST   /users(.:format)                                           users#create
             new_user GET    /users/new(.:format)                                       users#new
            edit_user GET    /users/:id/edit(.:format)                                  users#edit
                 user GET    /users/:id(.:format)                                       users#show
                      PATCH  /users/:id(.:format)                                       users#update
                      PUT    /users/:id(.:format)                                       users#update
                      DELETE /users/:id(.:format)                                       users#destroy

As you can see, there is no route called album, only one called user_album.

You can either draw a direct route to albums so your
routes.rb:

resources :users do
    resources :albums do
        resources :photos
    end
end
resources :albums

normally, you could also redirect to user_album giving als well the user as the album:

redirect_to user_album_url(@photo.album.user,@photo.album)

but in your models, there is no single user for one album. So your routes do not reflect your model relationship. (Your routes imply then an album belongs to one user, your models say that album has_and_belongs_to_many users)

Upvotes: 1

Related Questions