【发布时间】:2014-05-22 04:09:16
【问题描述】:
我有一个 form_for,它有一个 fields_for 用于创建双对象表单。第二个对象是具有回形针(ruby gem)属性的附件,用于添加属于第一个对象的照片。正在创建盒子模型,但据我所知,附件模型还不是很清楚。我的问题之一是 multipart => true 需要去哪里,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)
但这仍然不起作用。
【问题讨论】:
-
尝试在您的视图中打印出@box.id,看看它是否包含所需的id。
标签: object ruby-on-rails-4 paperclip form-for