我想知道如果有人可以帮助我的文件上传!Ruby on Rails回形针多重上传,嵌套属性
我想使用回形针上传多个图像并具有嵌套属性。
模式
class Trip < ActiveRecord::Base
has_many :trip_images, :dependent => :destroy
end
class TripImage < ActiveRecord::Base
belongs_to :trip
has_attached_file :photo, :styles => { :large => "800x800>", :medium => "500x500>", :thumb => "150x150#" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :photo, content_type: /\Aimage\/.*\Z/
end
控制器
def create
@trip = Trip.new(trip_params)
respond_to do |format|
if @trip.save
format.html { redirect_to @trip, notice: 'Trip was successfully created.' }
format.json { render :show, status: :created, location: @trip }
else
format.html { render :new }
format.json { render json: @trip.errors, status: :unprocessable_entity }
end
end
end
def trip_params
params.require(:trip).permit(
:user_id,
trip_images_attributes: [:id, :photo])
end
视图
<%= simple_form_for @trip, html: { multipart: true } do |f| %>
<%= f.simple_fields_for :trip_images do |p| %>
<%= p.file_field :photo, as: :file, multiple: true %>
<% end%>
<%= f.button :submit %>
<% end %>
我如何保存多张图片在我的旅行图像数据库?当我提交表单时,没有任何东西被保存到数据库中。
尝试改变'trip_images_attributes:[:ID,:照片])'来'trip_images_attributes:[:ID ,:photo => []])' – Pavan
并在'Trip'模型中添加'accep_nested_attributes_for:trip_images'。 – Pavan
您可以发布一条日志,请求创建操作? –