2013-07-29 29 views
0

我知道有圍繞這一話題幾個問題,一個是幾乎相同的,因爲我們遵循同樣的教程,但沒有一個答案是爲我工作。我跟着艾默生的走狗教程關於使用回形針附加多個圖像(http://emersonlackey.com/screencasts/rails-3-with-paperclip.mov)並且出現錯誤無法批量分配受保護的屬性:assets_attributes。當您查看代碼時,有一點需要注意,我每次發佈的圖片都只有圖片,因此圖片和資產的術語將被刪除,但現在需要其餘的網站才能使用。回形針錯誤 - 無法大規模指派保護屬性

我已經創建了一個資產ID,並將其添加到資產模型。另請注意,我有Pins而不是Posts。

在pin.rb型號:

class Pin < ActiveRecord::Base 
attr_accessible :description, :image, :image_remote_url, :Designer, :price, :retail_value, :condition, :lender_notes, :size, :material, :color, :classification, :item_category, :asset_attributes 

validates :user_id, presence: true 
validates :description, presence: true 
validates :Designer, presence: true 
validates :size, presence: true 
validates :color, presence: true 
validates :material, presence: true 
validates :price, presence: true 
validates :retail_value, presence: true 
validates :condition, presence: true 
validates :lender_notes, presence: true 
validates :classification, presence: true 
validates :item_category, presence: true 
validates_attachment :asset, presence: true, 
          content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'] }, 
          size: { less_than: 5.megabytes }        
belongs_to :user 
has_many :line_items 
has_many :assets 
accepts_nested_attributes_for :assets, :allow_destroy => true 
before_destroy :ensure_not_referenced_by_any_line_item 
has_attached_file :image, styles: { medium: "320x240"} 

# ensure that there are no line items referencing this product 
def ensure_not_referenced_by_any_line_item 
    if line_items.empty? 
     return true 
    else 
     errors.add(:base, 'Line Items present') 
    return false 
    end 
end 

def image_remote_url=(url_value) 
    self.image = URI.parse(url_value) unless url_value.blank? 
    super 
end 
end 

在asset.rb模式:

class Asset < ActiveRecord::Base 
attr_accessible :asset 

belongs_to :pin 
has_attached_file :asset, :styles => { :large => "640x480", :medium => "320x240", :thumb => "100x100>" } 

end 
在pins_controller.rb文件

class PinsController < ApplicationController 
before_filter :authenticate_user!, except: [:index] 

def index 
@pins = Pin.order("created_at desc").page(params[:page]).per_page(20) 

respond_to do |format| 
    format.html # index.html.erb 
    format.json { render json: @pins } 
    format.js 
end 
end 

def show 
@pin = Pin.find(params[:id]) 

respond_to do |format| 
    format.html # show.html.erb 
    format.json { render json: @pin } 
end 
end 

def new 
@pin = current_user.pins.new 
5.times { @pin.assets.build } 

respond_to do |format| 
    format.html # new.html.erb 
    format.json { render json: @pin } 
end 
end 

def edit 
@pin = current_user.pins.find(params[:id]) 
5.times { @pin.assets.build } 
end 

def create 
@pin = current_user.pins.new(params[:pin]) 

respond_to do |format| 
    if @pin.save 
    format.html { redirect_to @pin, notice: 'Pin was successfully created.' } 
    format.json { render json: @pin, status: :created, location: @pin } 
    else 
    format.html { render action: "new" } 
    format.json { render json: @pin.errors, status: :unprocessable_entity } 
    end 
end 
end 

def update 
@pin = current_user.pins.find(params[:id]) 

respond_to do |format| 
    if @pin.update_attributes(params[:pin]) 
    format.html { redirect_to @pin, notice: 'Pin was successfully updated.' } 
    format.json { head :no_content } 
    else 
    format.html { render action: "edit" } 
    format.json { render json: @pin.errors, status: :unprocessable_entity } 
    end 
end 
end 

def destroy 
@pin = current_user.pins.find(params[:id]) 
@pin.destroy 

respond_to do |format| 
    format.html { redirect_to pins_url } 
    format.json { head :no_content } 
    end 
end 
end 

在_form。 html.erb文件:

<%= simple_form_for(@pin, :html => { :multipart => true}) do |f| %> 
<%= f.full_error :asset_file_size, class: "alert alert-error" %> 
<%= f.full_error :asset_content_type, class: "alert alert-error" %> 
<%= f.fields_for :assets do |asset_fields| %> 
<%= asset_fields.file_field :asset %> 
<% end %> 

<div class="form-actions"> 
<%= f.button :submit, class: "btn btn-primary" %> 
</div> 

<% end %> 

謝謝!!!!!

回答

1

你說的錯誤是:

Can't mass-assign protected attributes: assets_attributes 

你沒有這個在attr_accessible定義;你有:asset_attributes但不:assets_attributes

嘗試將其更改爲複數形式。

+0

曾經嘗試都 - 如果我型我assets_attributes收到錯誤:NoMethodError在PinsController#爲#創建 未定義的方法'資產<針:0x00000102bbea10> –

+0

這意味着你還需要定義在一個'asset'方法Pin模型。在你的'attr_accessible' – tyler

+0

嘗試包括':assets_attributes'和':asset',不幸的是沒有變化 –

相關問題