ruby-on-rails - rails : Nested form with carrierwave

标签 ruby-on-rails mongoid nested-forms carrierwave

我不确定嵌入式类的 Controller 应该做什么。传递的参数确实显示了所有嵌入的类属性,包括图像属性,但只有非图像参数保存在数据库中。这告诉我问题不在于 ORM(在这种情况下为 Mongoid)的选择,而是与我使用载波的方式有关:

  Parameters: {"article"=>{"name"=>"New article", "comments_attributes"=>{"0"=>{"remote_image_url"=>"", "name"=>"Comment 1", "content"=>"comment content....", "image"=>#<ActionDispatch::Http::UploadedFile:0x10339d880 @headers="Content-Disposition: form-data; name=\"article[comments_attributes][0][image]\"; filename=\"dh.png\"\r\nContent-Type: image/png\r\n", @original_filename="dh.png", @tempfile=#<File:/var/folders/A1/A1SUPUTUFA8BYB5j+RD2L++++TI/-Tmp-/RackMultipart20120228-21178-1vckii1-0>, @content_type="image/png">}}, "content"=>"article content"}, "commit"=>"Create Article", "authenticity_token"=>"i14YuJs4EVKr5PSEw9IwKXcTbQfOP4mjbR95C75J2mc=", "utf8"=>"\342\234\223"}
MONGODB (89ms) freedb['system.namespaces'].find({})
MONGODB (0ms) freedb['articles'].insert([{"name"=>"New article", "comments"=>[{"name"=>"Comment 1", "_id"=>BSON::ObjectId('4f4daf6a58001652ba000012'), "content"=>"comment content...."}], "_id"=>BSON::ObjectId('4f4daf6958001652ba000011'), "content"=>"article content"}])

父模型:

class Article
  include Mongoid::Document
  field :name, :type => String
  field :content, :type => String

  embeds_many :comments

  accepts_nested_attributes_for :comments
end

child 模型:

require 'carrierwave/mongoid'

class Comment
  include Mongoid::Document
  field :name, :type => String
  field :content, :type => String

  field :image, :required => true
  field :remote_image_url

  embedded_in :article, :inverse_of => :comments
  mount_uploader :image, ImageUploader
end

父 Controller :

  def new
    @article = Article.new
    @article.comments.build
  end

  def create
    @article = Article.new(params[:article])
  end

父表单:

<%= form_for(@article, :html => {:multipart => true}) do |f| %>

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

    <%= f.fields_for :comments do |c| %>
    <p>
        <%= c.label :name %>
        <%= c.text_field :name %>
    </p>

    <p>
        <%= c.label :image, "Select Screenshot from your Computer"%><br />
        <%= c.file_field :image %>
    </p>

    <p>
        <%= c.label :remote_image_url, "or URL from the interweb"%><br />
        <%= c.text_field :remote_image_url %>
    </p>

    <% end %>


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

最佳答案

Carrierwave 使用一些回调将图像和数据保存在模型中。默认情况下,嵌入式模型没有回调执行。您需要明确说明您的嵌入需要执行他的回调。

为此,请使用 cascade_callbacks: true 选项。

embeds_many :comments, cascade_callbacks: true

关于ruby-on-rails - rails : Nested form with carrierwave,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9493808/

相关文章:

ruby-on-rails - 尝试创建新客户端时,Mongoid 找不到名称为 : 'default' 的客户端配置

ruby-on-rails - 使用 fields_for 时重复表单字段

ruby-on-rails - Rails 嵌套表单属性不保存

javascript - link_to_add_fields/不显眼的javascript rails 4

iphone - 使用 ASIHTTPRequest 从 iPhone 上传到 Ruby on Rails

mysql - 使用 LDAP/AD 登录身份验证时,如果使用 AD 记录不存在用户行,如何创建用户行

ruby-on-rails - Rails 4 - 自定义字体

rspec - 如何测试 Mongoid 将生成的查询类型?

ruby-on-rails - 使用 Mongoid 从数据库延迟下载字段,或处理大型文档

ruby-on-rails - 如何使用 FactoryGirl 创建嵌入式文档?