嗨我目前收到表單提交錯誤。我的應用程序基本上與用戶,專輯,圖片(用於圖片上傳)。然而,當我嘗試創建一個新的專輯,這是給我的錯誤:Rails:「狀態」未正確插入params?
的ActiveRecord :: RecordNotFound在AlbumsController#顯示
無法與ID查找唱片= 123 [WHERE 「album_users」「。 user_id「= 29 AND(status ='accepted')]
問題在於每個專輯可以有多個所有者,因此在創建專輯的表單中有一個複選框部分,您可以突出顯示您的朋友姓名和「邀請他們」成爲所有者。這就是爲什麼我的創建函數看起來有點奇怪。這裏的問題是獲取:status =>'accepted'來正常插入。請幫忙!
相冊控制器
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
用戶模型:
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
端
用@ user.albums.create替換'@ user.albums.build' – apneadiving