以下是我的控制器方法部分:
class ProductAttachmentsController < ApplicationController
def create
@product_attachment = ProductAttachment.new(product_attachment_params)
session[:product_id] --> return product_id
respond_to do |format|
if @product_attachment.save
format.html { redirect_to @product_attachment, notice: 'Product attachment was successfully created.' }
# format.json { render :show, status: :created, location: @product_attachment }
format.json {render :json => @product_attachment}
else
format.html { render :new }
format.json { render json: @product_attachment.errors, status: :unprocessable_entity }
end
end
end
end
模型关系:
class Product < ActiveRecord::Base
has_many :product_attachments, dependent: :destroy
end
class ProductAttachment < ActiveRecord::Base
mount_uploader :attachment, AttachmentUploader
belongs_to :product
end
用户上传图片后,product_attachments
在表格中插入新行,现在如何将product_id
与附件一起包含?
谢谢!
修改
查看表单:
<section class="photos-section <%= 'hide' unless @show_section == 'photo' %>">
<%= form_for(@product) do |f| %>
<div class="row">
<div class="col-xs-12 col-sm-12 col-lg-12">
<hr>
<div class="form-group">
<div class="fileUpload btn btn-large btn-upload">
<span><i class="fa fa-upload" aria-hidden="true" id="file_upload"></i> Add Photos</span>
<input name="file_attachment" type="file" id="file_upload" class="upload">
</div>
</div>
<hr>
</div>
</div>
<div class="row attachments-container">
<div class="row">
<div class="col-4 row-space-2 h5 invisible" id="js-first-photo-text">
Your first photo appears in search results!
</div>
</div>
<% if !@product.new_record? %>
<% @product.product_attachments.each do |attachment| %>
<div class="col-xs-6 col-md-4 form-group grayscale">
<img src="<%= attachment.attachment.small.url %>" class="upload-photo-image">
<input type="hidden" name="product_attachment[id][]" value="<%= attachment.id %>">
<button class="delete-photo-btn overlay-btn js-delete-photo-btn delete-attachment" data-toggle="tooltip" data-placement="top" title="Delete" data-photo-id="143275119">
<i class="fa fa-trash-o"></i>
</button>
<button class="cover-photo-btn cover-overlay-btn js-delete-photo-btn cover-attachment" data-toggle="tooltip" data-placement="top" title="Set as cover photo" data-photo-id="143275119">
<i class="fa fa-picture-o"></i>
</button>
</div>
<% end %>
<% end %>
<div class="col-xs-6 col-md-4 form-group">
<div class="thumbnail panel photo-item empty-photo-frame" name="empty-photo-frame">
<img src="<%= asset_url('add-image-placeholder.png') %>">
</div>
</div>
</div>
<hr>
<div class="row">
<div class="col-xs-12 next-bottom paddingzero">
<div class="col-xs-6">
<% if @product.new_record? %>
<a href="#" class="btn-back">Back</a>
<% else %>
<a href="/products/<%= @product.id %>/edit_location" class="btn-back">Back</a>
<% end %>
</div>
<div class="col-xs-6">
<% if @product.new_record? %>
<a href="" class="btn btn-next pull-right">See Next</a>
<a href="/products/<%= @product.id %>/edit_price" class="btn btn-next pull-right">See Next</a>
<% else %>
<input type="hidden" name="step-param" value="photo">
<%= f.submit 'See Next', :class => 'btn btn-next pull-right photo-submit' %>
<% end %>
</div>
</div>
</div>
<% end %>
</section>
答案 0 :(得分:1)
您可以查看product_attachment_params
我认为此参数中没有product_id
字段,因为我在表单中看不到product_id
字段。
你也可以使用
@product.product_attachments.build(product_attachment_params)
答案 1 :(得分:0)
在控制器中执行以下操作:
class ProductAttachmentsController < ApplicationController
def create
product_id = session[:product_id]
# Look for the product by product_id and create your attachment from there.
@product_attachment = Product.find(product_id).build(product_attachment_params)
respond_to do |format|
if @product_attachment.save
format.html { redirect_to @product_attachment, notice: 'Product attachment was successfully created.' }
# format.json { render :show, status: :created, location: @product_attachment }
format.json {render :json => @product_attachment}
else
format.html { render :new }
format.json { render json: @product_attachment.errors, status: :unprocessable_entity }
end
end
end
end