0

我創建了相冊模型和照片模型,並將其添加到現有的rails應用程序中。我已將相片模型歸入專輯模型,相冊模型屬於現有的屬於用戶模型的配置文件模型。我不知道我是否將它們與錯誤聯繫起來,以及爲什麼會出現錯誤。如何更正此模型關聯?

值得注意的是,當我訪問URL /相冊時,所有內容都可以正常工作,但當我轉到URL/profiles/1時(下面的代碼粘貼在views/profile的show.html.erb文件中/文件夾),那麼我得到下面的錯誤。

這是一個簡單的問題,我無法解決。這四個模型文件如下:

Album.rb:

class Album < ActiveRecord::Base 
    belongs_to :profile 
    has_many :photos, :dependent => :destroy 
    accepts_nested_attributes_for :photos, :allow_destroy => true 
end 

Profile.rb:

​​

Photo.rb:

require 'paperclip' 
class Photo < ActiveRecord::Base 

    belongs_to :album 
    has_attached_file :upload, 
       :url => "/images/:id/:style/:basename.:extension", 
       :path => ":rails_root/public/images/:id/:style/:basename.:extension", 
       :styles => { 
        :thumb => "75x75>", 
        :small => "200x200>" 
       } 
    #add in any validations you may want 
end 

User.rb:

class User < ActiveRecord::Base 

    include Gravtastic 
    gravtastic :size => 120 

    # associations 
    has_many :albums 
    has_many :photos, :through => :albums 
    has_many :authorizations, :dependent => :destroy 
    has_one :profile, :dependent => :destroy 
    has_many :resumes, :dependent => :destroy, :order => 'created_at DESC' 
    has_many :thoughts, :dependent => :destroy, :order => 'created_at DESC' 
    has_many :user_threads, :dependent => :destroy, :order => 'created_at ASC' 

    accepts_nested_attributes_for :profile 

    # virtual attributes 
    attr_accessor :first_name, :last_name 

    # validations 
    validates_presence_of :first_name 
    validates_presence_of :last_name 
    validates_length_of :username, :minimum => 4, :message => " is too short" 
    validates :email, :email => {:message => " is not valid"} 
    validates_uniqueness_of :email, :case_sensitive => false 
    validates_uniqueness_of :username, :case_sensitive => false 
    validates_length_of :password, :minimum => 4, :message => " is too short" 

    # authlogic 
    acts_as_authentic do |config| 
    config.crypto_provider = Authlogic::CryptoProviders::MD5 
    config.maintain_sessions = false 
    config.validate_email_field = false 
    config.validate_login_field = false 
    config.validate_password_field = false 
    config.login_field = :email 
    config.validate_login_field = false 
end 

    def self.create_from_hash!(hash) 
    user = User.new(:username => Time.now.to_i, :email => '', :auth_provider => hash['provider']) 
    user.save(:validate => false) 
    if hash['provider'].downcase == 'twitter' 
    user.profile = Profile.create(:first_name => Twitter::Client.new.user(hash['user_info'] ['nickname'].to_s).name) 
else 
    user.profile = Profile.create(:first_name => hash['user_info']['first_name'], :last_name => hash['user_info']['last_name']) 
end 
user 
end 

    def deliver_password_reset_instructions! 
    reset_perishable_token! 
    UserMailer.deliver_password_reset_instructions(self) 
    end 

    def activate! 
    self.active = true 
    save(false) 
    end 

    def deliver_activation_instructions! 
    reset_perishable_token! 
    UserMailer.deliver_activation_instructions(self) 
    end 

end 

輪廓控制器具有這個片段:

def show 
    @user = User.find_by_username(params[:id]) 
    @profile = @user.profile 

    @location = Profile.get_location(@profile) 

    @resumes = @user.resumes 

    @albums = @user.albums 

    @photos = @user.photos 

    @thoughts = @user.thoughts 

    @shouts = UserThread.find_profile_shouts(@profile) 

    @shouters = UserThread.find_shouters(@shouts) 

    @user_thread = UserThread.new 
    end 

的看法有這樣的:

<div id="profile_right_col"> 
    <h2>Albums</h2> 
    <p> 
     <b>Name:</b> 
     <%= @albums %><br /> 

     <% @albums.photos.each do |photo| %> 
     <h3><%= photo.title %></h3> 
     <%= image_tag photos.upload.url(:small) %> 
     <% end %> 
    </p> 

    <%= link_to 'Edit', edit_album_path(@albums) %> | 
    <%= link_to 'Back', albums_path %> 
</div> 

動作控制器異常顯示:

ActiveRecord::StatementInvalid in Profiles#show 

Showing /Users/pawel/Ruby/Apps/cvf/app/views/profiles/show.html.erb where line #137 raised: 

SQLite3::SQLException: no such column: albums.user_id: SELECT  "albums".* FROM  "albums" WHERE  ("albums".user_id = 4) 

Extracted source (around line #137): 

<h2>Albums</h2> 
<p> 
<b>Name:</b> 
<%= @albums %><br /> 

<% @albums.photos.each do |photo| %> 
<h3><%= photo.title %></h3> 

回答

0

它在我將Paperclip::Railtie.insert添加到我的application.rb後工作。

0

你不必在你的節目中定義@album變量行動(你有@albums和你需要通過@albums數組的視圖)。所以它的價值是零,它沒有方法照片。

+0

對不起,這是我的錯字。我編輯過它。 – Simpleton

+0

@albums是一個數組,所以你應該使用'@ albums.each'來完成它。 – Hck

+0

它引發一個異常:'SQLite3 :: SQLException:no such column:albums.user_id:SELECT「albums」。* FROM「albums」WHERE(「albums」.user_id = 4)' - 如何遷移?謝謝 – Simpleton