我有一个form_for,它有一个用于双重对象表单创建的fields_for。第二个对象是具有回形针(ruby gem)属性的附件,用于添加属于第一个对象的照片。正在创建盒子模型,但附件模型并不是我所知道的。我的一个问题是multipart =>真的需要去,form_for,fields_for或两者兼而有之?
编辑:似乎box_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为空白,但在模型中进行验证后,我不会收到任何错误。
这是我的表单代码:
<%= 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
我还尝试更改box_controller以通过
创建附件@attachment = Attachment.new(attachment_params)
但仍然没有用。
答案 0 :(得分:0)
尝试在视图中打印@box.id
,看看它是否包含所需的id
,并将box_id
与:screenshot
一起发送
def attachment_params
params.require(:attachment).permit(:box_id, :screen_shot)
end