还有另一种方法来使用paperclip存储多个图像而不是我使用的那个,因为它看起来很难看
this is how the form looks like
这是代码
应用/视图/帖/ _form.html.erb
<%= simple_form_for @post, html: { class: 'form-horizontal', multipart: true } do |f| %>
<%= f.input :title %>
<%= f.input :description %>
<%= f.simple_fields_for :pictures do |builder| %>
<% if builder.object.new_record? %>
<%= builder.input :image, :input_html => { :multiple => true } %>
<% end %>
<% end %>
<%= f.input :price %>
<%= f.input :city %>
<%= f.input :phone %>
<%= f.button :submit %>
<% end %>
这是posts_controller
中的新操作和创建操作应用/控制器/ posts_controller.rb
def new
@post = current_user.posts.build
@post.pictures.build
end
def create
@post = current_user.posts.build(post_params)
if @post.save
params[:image].each do |picture|
@post.images.create(:image=> picture)
end
redirect_to root_path
else
redirect_to new_post_path
end
end
private
def post_params
params.require(:post).permit( :title, :price, :description, :phone, :image, :city, pictures_attributes: [:image] )
end
现在出现错误未经许可的参数:图片
Started POST "/posts" for 127.0.0.1 at 2016-08-18 07:04:27 +0100
Processing by PostsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"1M2k+ZnouBRdtP6/EDHtLtewiwiS32uSLcZldPNghFt/8n81txtUAAdbKzlCpn6x5l4RJNZYYqV7OVTs6pyvBQ==", "post"=>{"title"=>"setst", "description"=>"setset", "pictures_attributes"=>{"0"=>{"image"=>[#<ActionDispatch::Http::UploadedFile:0xa337e44 @tempfile=#<Tempfile:/tmp/RackMultipart20160818-20404-fu0x0v.jpg>, @original_filename="column-01.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"post[pictures_attributes][0][image][]\"; filename=\"column-01.jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0xa337e30 @tempfile=#<Tempfile:/tmp/RackMultipart20160818-20404-1sidf3n.png>, @original_filename="fiy_away.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"post[pictures_attributes][0][image][]\"; filename=\"fiy_away.png\"\r\nContent-Type: image/png\r\n">]}}, "price"=>"3", "city"=>"setset", "phone"=>""}, "commit"=>"Create Post"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 2], ["LIMIT", 1]]
Unpermitted parameter: image
(0.2ms) begin transaction
(0.1ms) rollback transaction
Redirected to http://localhost:3000/posts/new
Completed 302 Found in 26ms (ActiveRecord: 0.8ms)
我想只在文件字段上使用来添加所有图像并将它们存储为数组是可能的!
答案 0 :(得分:0)
我发现这个问题的解决方案我所做的就是删除了simple_form并使用了普通的form_for
<%= form_for @post, html: { class: 'form-horizontal', multipart: true } do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :description %>
<%= f.text_field :description %>
<%= f.fields_for :pictures do |builder| %>
<%= f.label :pictures, :class => 'control-label' %>
<%= file_field_tag "images[]", type: :file, multiple: true %>
<% end %>
<%= f.label :price %>
<%= f.text_field :price %>
<%= f.label :city %>
<%= f.text_field :city %>
<%= f.label :phone %>
<%= f.text_field :phone %>
<%= f.submit nil, :class => 'btn btn-primary' %>
<% end %>
以及posts_controller中的创建操作
def create
@post = current_user.posts.build(post_params)
if @post.save
if params[:images]
#===== The magic is here ;)
params[:images].each { |image|
@post.pictures.create(image: image)
}
end
redirect_to root_path
else
redirect_to new_post_path
end
end
答案 1 :(得分:0)
我在使用嵌套资源的Rails 5上遇到了类似的问题,我发现解决方案只在 file_field 上添加 multiple:true 。
我有两个型号Medic和Patient。患者是Medic的嵌套资源,我使用Devise,因此我可以访问@patient = current_medic.patient
在视图中:
<%= form_for @patient, url: patients_path, html: { multipart: true } do |f| %>
<%= f.file_field :image, multiple: true %>
<% end %>
编辑 - 创建方法:
def create
@patient = current_medic.patients.new(patient_params)
respond_to do |format|
if @patient.save
format.html { redirect_to @patient, :flash => { :success => 'Patient was successfully created.' } }
else
format.html { render :new }
end
end
end
希望这有帮助!