2012-01-10 88 views
1

使用Rails 3.1.1,1.9.2紅寶石, 寶石:寶石 'HAML',創業板simple_form「,創業板 'AWS-SDK',
寶石「回形針」,:git的=> 「GIT中://github.com/thoughtbot/paperclip.git」
插件:country_select:GIT中://github.com/rails/country_select.git回形針 「寶石」 的Rails 3.1未定義的方法模型文件名

有一個問題上傳/顯示圖像推送到亞馬遜S3通過回形針(GEM) 錯誤:undefined方法`avatar_file_name'的#Player:0x00000102aff228

大部分我是在git上的例子對於回形針 https://github.com/thoughtbot/paperclip

這裏-hub頁是我在我的代碼:

遷移:20111224044508_create_players.rb

class CreatePlayers < ActiveRecord::Migration 
    def change 
    create_table :players do |t| 
     t.string :first_name 
     t.boolean :first_name_public, :default => false 
     ... 
     t.string :website 
     t.boolean :website_public, :default => false 
     t.has_attached_file :avatar 

     t.timestamps 
    end 
    end 
end 

型號:Player.rb:

class Player < ActiveRecord::Base 
    attr_accessible :first_name, ... :website 
    validates_presence_of :username, :email 

    has_attached_file :avatar, 
        :styles => { :medium => "300x300>", :thumb => "100x100>" }, 
        :storage => :s3, 
        :s3_credentials => "#{Rails.root}/config/s3.yml", 
        :path => ":class/:id/:style/:filename" 

    {Unrelated validations} 
end 

S3文件:s3.yml

development: 
    bucket: voh_development 
    access_key_id: ********************* 
    secret_access_key: ******************** 
staging: 
    bucket: voh_staging 
    access_key_id: ********************* 
    secret_access_key: ******************** 
production: 
    bucket: voh_production 
    access_key_id: ********************* 
    secret_access_key: ******************** 

控制器:players_controller.rb

class PlayersController < ApplicationController 

    def create 
    @player = Player.create(params[:player]) 

    if @player.save 
     redirect_to players_path, :notice => "Player Created"; 
    else 
     render :action => 'new' 
    end 
    end 

    {basic restful} 

end 

瀏覽: Edit.html.haml + New.html.haml

= simple_form_for @player do |f| 
    = f.input :first_name 
    ... 
    = f.input :website 
    = f..file_field :avatar 
    .input_div  
    = f.button :submit 

index.html.haml

... 
    %td Avatar 
    %td First Name 
    ... 
    %td Actions 
    - @players.each do |player| 
    %tr 
     %td 
     = image_tag @player.avatar.url(:thumb) 
     %td 
     = player.first_name 
     ... 
     %td 
     = link_to ' Show ', player_path(player.id) 
     | 
     = link_to ' Edit ', edit_player_path(player.id) 

表演。 html.haml

= image_tag @user.avatar.url 
%br 
= @player.first_name 
... 

研究: 我發現很多事情要做遷移的pluging和genration但是這一切似乎老了。他們中的大多數人建議在遷移過程中爲這四個屬性進行優化。然而,它似乎應該已被替換爲一行t.has_attached_file:頭像。

我有一個rails 3.0項目,這工作。我能夠上傳產品並將其拉回。 (必須使用建議的image_tag @ icon.avatar.url並將其轉換爲%img {:src => URI.unescape(icon.icon.url)},但這是一個不同的問題。)

回答

1

TLDR:固定在@player => player的index.html.haml中輸入錯誤,將頭像添加到attr_accessible。

我早上醒來的時候,有一個不同的錯誤。

代替:未定義的方法Avatar_file_name'

我:未定義的方法avatar' for nil:NilClass

正是這個錯誤,導致買在我的代碼簡單類型。我用vairable,而不是我應該一直在使用。每個變量的一個實例(index.html.haml:9)

現在的應用程序是不是犯錯誤了,但該文件還沒有上傳。 在開發日誌中,我找到了這個。 (我沒有看這裏,我第一次公佈)

警告:不能大規模指派保護屬性:頭像

然後我去並補充說:阿凡達attr_accessible,一切都開始工作。

不知道這是否應該是必需的,但我確實看到他們已經更新了S3頭文件,併成爲昨天的proc文件。 https://github.com/thoughtbot/paperclip/tree/master/test/storage

我不打算把它關閉。將有一個編輯,因爲我打算玩這個版本,並迅速報告我的發現今天。然後我會把它關閉。

編輯︰ 我試着切換回2.4.5,我得到的錯誤,讓我切換到首先拉主人。嘗試使用t.has_attached_file:avatar進行遷移時,遷移失敗並出現以下錯誤。對於#ActiveRecord :: ConnectionAdapters :: PostgreSQLAdapter :: TableDefinition

未定義的方法`has_attached_file」:0x00000105053600

我想我會從主拉棒。

相關問題