【问题标题】:attr_accessible with paperclip multiple picture uploadsattr_accessible 与回形针多张图片上传
【发布时间】:2011-04-13 17:14:35
【问题描述】:

我按照教程 here 进行操作,结果一切顺利......直到我尝试将 attr_accessible 添加到文章模型中。提前致谢。 相关代码如下:

app/models/user.rb

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

app/models/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

app/views/users/_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 %>

【问题讨论】:

    标签: paperclip attr-accessible


    【解决方案1】:

    在尝试了各种排列并浏览了相关帖子之后,终于抓住了这几天一直在躲避我的小精灵。我需要的只是将 :assets_attributes 添加到用户模型中的 attr_accessible 列表中。感谢阅读!

    attr_accesible for nested objects

    【讨论】:

      猜你喜欢
      • 2013-05-29
      • 2015-05-09
      • 1970-01-01
      • 2016-09-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多