2012-10-11 20 views
0

嗨我目前收到表單提交錯誤。我的應用程序基本上與用戶,專輯,圖片(用於圖片上傳)。然而,當我嘗試創建一個新的專輯,這是給我的錯誤: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 

+0

用@ user.albums.create替換'@ user.albums.build' – apneadiving

回答

1

線:

@album = @user.albums.build(params[:album], :status => 'accepted') 

使得沒有SENCE。只有params[:album]將受到尊重。用散#合併,合併:statusparams[:album](不修改最後一個):

@album = @user.albums.build(params[:album].merge(:status => 'accepted')) 

當心!

你指定你的albums協會的條件:

has_many :albums, :through => :album_users, :conditions => "status = 'accepted'" 

但它不會被考慮在做相關的相簿(例如@user.album.build)。你必須指定它如哈希

has_many :albums, :through => :album_users, :conditions => {status: 'accepted'} 

與尊重。這樣你就不需要通過調用build:status的值將自動設置爲'accepted'

+0

哎感謝您的建議jdoe。但現在我得到另一個錯誤:'SQLite3 :: SQLException:沒有這樣的列:albums.status:SELECT COUNT(*)FROM「albums」INNER JOIN「album_users」ON「albums」。「id」=「album_users」。 「album_id」WHERE「album_users」。「user_id」= 29 AND(「albums」。「status」='accepted')'。 'status'列是否必須在專輯表中? Cuz我認爲它應該放在album_user模型中 – Edmund

+0

@Edmund如果你必須爲連接模型傳遞額外的選項(在我們的例子中是AlbumUser),那麼你可以手動創建關係:'AlbumUser.create status:'accepted',user: @用戶,相冊:@相冊「。你的代碼不清楚你的「狀態」屬於哪個模型。 – jdoe