attr_accessible 用回形针多图片上传

发布于 2024-10-31 22:16:05 字数 2749 浏览 3 评论 0原文

我按照这里的教程进行操作,一切都很好......直到我尝试将 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/视图/用户/_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 %>

I followed the tutorial here and everything turned out nicely.. until I tried adding attr_accessible to the article model. Thanks in advance.
Here's the related code:

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 %>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把昨日还给我 2024-11-07 22:16:05

在尝试了各种排列并浏览相关帖子后,终于抓住了过去几天一直困扰着我的小精灵。我所需要做的就是将 :assets_attributes 添加到用户模型中的 attr_accessible 列表中。感谢您的阅读!

嵌套对象的 attr_accessible

After trying various permutations and going through related posts, finally caught the gremblin that's been eluding me the past few days. All I needed is to add the :assets_attributes to the list of attr_accessible in the user model. Thanks for reading!

attr_accesible for nested objects

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文