Hi I’m currently receiving an error on form submission. My app is basically one with users, albums, pics (for picture uploading). However, when I try to create a new album, it is giving me the error:
ActiveRecord::RecordNotFound in AlbumsController#show
Couldn’t find Album with id=123 [WHERE “album_users”.”user_id” = 29 AND (status = ‘accepted’)]
The catch is that there can be multiple owners for each album, so in the form to create an album there is a check box portion where you highlight your friends names and “invite them” to be an owner. This is why my create function looks a little weird. The issue here is getting the :status => ‘accepted’ to insert normally. Please help!
albums controller
def create
@user = User.find(params[:user_id])
@album = @user.albums.build(params[:album], :status => 'accepted')
@friends = @user.friends.find(params[:album][:user_ids])
for friend in @friends
params[:album1] = {:user_id => friend.id, :album_id => @album.id, :status => 'pending'}
AlbumUser.create(params[:album1])
end
#the next line is where the error occurs. why???
if @user.save
redirect_to user_album_path(@user, @album), notice: 'Album was successfully created.'
else
render action: "new"
end
end
def show
@user = User.find(params[:user_id])
@album = @user.albums.find(params[:id]) #error occurs on this line
end
user model :
class User < ActiveRecord::Base
has_secure_password
attr_accessible :email, :name, :password, :password_confirmation, :profilepic
validates_presence_of :password, :on => :create
validates_format_of :name, :with => /[A-Za-z]+/, :on => :create
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create
validates_length_of :password, :minimum => 5, :on => :create
# validates :album, :uniqueness => true
has_many :album_users
has_many :albums, :through => :album_users, :conditions => "status = 'accepted'"
has_many :pending_albums, :through => :album_users, :source => :album, :conditions => "status = 'pending'"
accepts_nested_attributes_for :albums
has_many :friendships, :dependent => :destroy
has_many :friends, :through => :friendships, :conditions => "status = 'accepted'"
has_many :requested_friends, :through => :friendships, :source => :friend, :conditions => "status = 'requested'", :order => :created_at
has_many :pending_friends, :through => :friendships, :source => :friend, :conditions => "status = 'pending'", :order => :created_at
has_attached_file :profilepic
before_save { |user| user.email = email.downcase }
def name_with_initial
"#{name}"
end
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
The line:
makes no sence. Only
params[:album]will be respected. Use Hash#merge to combine:statusandparams[:album](without modifying the last one):Beware!
You’ve specified conditions on your
albumsassociation:But it won’t be taken into consideration on making associated albums (like
@user.album.build). You have to specify it like a hash:to be treated with respect. This way you won’t be needed to pass
:statusinbuildcalls. The value of:statuswill be set automatically to'accepted'.