Object 在哪里放置:html=>{:multipart=>true},格式为&fields\u for?

Object 在哪里放置:html=>{:multipart=>true},格式为&fields\u for?,object,ruby-on-rails-4,paperclip,form-for,Object,Ruby On Rails 4,Paperclip,Form For,我有一个表单,它有一个字段,用于创建双对象表单。第二个对象是具有回形针ruby gem属性的附件,用于添加属于第一个对象的照片。盒子模型正在创建中,但附件模型并不是我能说的那么远。我的一个问题是multipart=>true需要到哪里去,表单、字段或两者 编辑:由于框尚未创建,框id似乎无法作为参数传递。那么,如何使附件归属于控制器中的框?我想 @box.attachments.build(params) 将做出正确的归因,但这不起作用 我需要在这里使用嵌套属性吗 以下是控制台日志: Proc

我有一个表单,它有一个字段,用于创建双对象表单。第二个对象是具有回形针ruby gem属性的附件,用于添加属于第一个对象的照片。盒子模型正在创建中,但附件模型并不是我能说的那么远。我的一个问题是multipart=>true需要到哪里去,表单、字段或两者

编辑:由于框尚未创建,框id似乎无法作为参数传递。那么,如何使附件归属于控制器中的框?我想

@box.attachments.build(params)
将做出正确的归因,但这不起作用

我需要在这里使用嵌套属性吗

以下是控制台日志:

Processing by BoxesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"0vka+nZDc56OdISMctWoJjY8CH+TR20PHBl8oVglD5A=", "box"=>{"user_id"=>"45", "name"=>"asdfasdf", "origin"=>"asdfasdf", "description"=>"asdfasdf"}, "attachment"=>{"box_id"=>"", "screen_shot"=>#<ActionDispatch::Http::UploadedFile:0x007fc3d8202990 @tempfile=#<Tempfile:/tmp/RackMultipart20140521-11119-37nj3s>, @original_filename="P5110017.JPG", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"attachment[screen_shot]\"; filename=\"P5110017.JPG\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Share the Love!"}

但这仍然不起作用。

尝试在视图中打印@box.id,查看它是否包含所需的id,并将您的box\u id与:屏幕截图一起发送


尝试在您的视图中打印@box.id,看看它是否包含所需的id。好的,我一直在尝试不同的事情,我最初在附件参数中有:box\u id。否则,我认为你是对的。新页面上不存在框id,因为尚未创建该框。因此,它不能作为参数传递。
  <%= form_for @box, :html => { :multipart => true } do |f| %>
    <%= f.hidden_field :user_id, :value => current_user.id %>

    <%= f.label :name, "Name" %><br>
    <%= f.text_field :name, autofocus: true %><br>

    <%= f.label :origin, 'Website' %><br>
    <%= f.text_field :origin %><br>

    <%= f.label :description %><br>
    <%= f.text_area :description, cols: "30", rows: "4" %>

    <%= fields_for @attachment do |ff| %>
        <%= ff.hidden_field :box_id, :value => @box.id %>
        <%= ff.file_field :screen_shot, id:"attachment", :required => true %>
    <% end %>

    <%= f.submit "Create!" %>
  <% end %>
class BoxesController < ApplicationController

  def new
    @box = Box.new
    @attachment = Attachment.new
  end

  def create
    @box = current_user.boxes.build(boxes_params)
    @attachment = @box.attachments.build(attachment_params)
    if @box.save
      flash[:success] = "Box created"
      redirect_to @box
    else
      flash[:error] = "There was an error"
      redirect_to 'new'
    end 
  end

  def update
    @box = Box.find(params[:id])
    if @box.update_attributes(boxes_params)
      flash[:success] = "Box updated"
      redirect_to @box
    else
      render 'edit'
    end
  end

  def destroy
    @box = Box.find(params[:id])
    @box.destroy
    flash[:success] = "Box deleted."
    redirect_to root_url
  end

  private

    def boxes_params
      params.require(:box).permit(:description, :name, :origin, :attachment)
    end

    def attachment_params
      params.require(:attachment).permit(:screen_shot, :box_id)
    end
end
  class Box < ActiveRecord::Base
    belongs_to :user
    has_many :comments
    has_many :attachments, :dependent => :destroy, :limit => 4
    is_impressionable
    validate :attachments_count_within_limit, :on => :create
    validates :user_id, presence: true
    validates_presence_of :name
    validates_length_of :description, :maximum => 1000
class Attachment < ActiveRecord::Base
  belongs_to :box
  has_attached_file :screen_shot, :styles => { :medium => "300x300>", :thumb => "100x100>" }
  validates_attachment_content_type :screen_shot, :content_type => /\Aimage\/.*\Z/
  validates :box_id, presence: true
end
@attachment = Attachment.new(attachment_params)
def attachment_params
   params.require(:attachment).permit(:box_id, :screen_shot)
end