我想知道如果有人可以幫助我的文件上傳!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
您可以發佈一條日誌,請求創建操作? –