2011-04-13 96 views
0

我跟着教程here和一切都很好..直到我試圖將attr_accessible添加到文章模型。提前致謝。 下面是相關的代碼:attr_accessible與回形針多張圖片上傳

應用程序/模型/ user.rb

class User < ActiveRecord::Base 
    attr_accessible :name, :email 
    has_many :assets, :dependent => :destroy 
    accepts_nested_attributes_for :assets, :allow_destroy => true 
end 

應用程序/模型/ asset.rb

class Asset < ActiveRecord::Base 
    attr_accessible :user_id, :image 
    belongs_to :user 
    has_attached_file :image, 
    :styles => { 
     :thumb=> "100x100#", 
     :small => "300x300>", 
     :large => "600x600>" 
    } 
end 

DB/schema.rb

create_table "assets", :force => true do |t| 
    t.integer "user_id" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.string "image_file_name" 
    t.string "image_content_type" 
    t.integer "image_file_size" 
    end 

    create_table "users", :force => true do |t| 
    t.string "name" 
    t.string "email" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

應用程序/視圖/用戶/ _form.html.erb

<%= form_for(@user, :html => { :multipart => true }) do |f| %> 
    <% if @user.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2> 

     <ul> 
     <% @user.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <div class="field"> 
    <%= f.label :name %><br /> 
    <%= f.text_field :name %> 
    </div> 
    <div class="field"> 
    <%= f.label :email %><br /> 
    <%= f.text_field :email %> 
    </div> 

    <div class="newPaperclipFiles"> 
    <%= f.fields_for :assets do |asset| %> 
     <% if asset.object.new_record? %> 
     <%= asset.file_field :image %> 
     <% end %> 
    <% end %> 
    </div> 

    <div class="existingPaperclipFiles"> 
    <% f.fields_for :assets do |asset| %> 
     <% unless asset.object.new_record? %> 
     <div class="thumbnail"> 
      <%= link_to(image_tag(asset.object.image.url(:thumb)), asset.object.image.url(:original)) %> 
      <%= asset.check_box :_destroy %> 
     </div> 
     <% end %> 
    <% end %> 
    </div> 

    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

回答

2

嘗試不同的排列組合,並通過相關的帖子會後,終於抓住了那個一直逃避我這幾天的gremblin。我所需要的只是將:assets_attributes添加到用戶模型中的attr_accessible列表中。謝謝閱讀!

attr_accesible for nested objects