我有一个 Recipe 模型和 RecipePhoto 模型。我想在创建新食谱时显示缩放图像的缩略图。我通过 jquery-file-upload 上传照片。问题是我不知道如何获取上传照片的 ID。
例子:
用户正在创建新配方。他加载了一张照片并在服务器上裁剪,他在提交食谱之前看到了缩略图预览。
recipes_controller.rb
def new
@recipe = Recipe.new
@recipe_photo = RecipePhoto.new
end
食谱/new.html.slim
= simple_form_for @recipe, :html => {:class => 'add-recipe-form'} do |recipe|
.form-inputs
= recipe.input :title
.form-actions
= recipe.submit "Create recipe"
= form_for @recipe_photo do |f|
.form-inputs
= f.label :image, "Upload paintings:"
= f.file_field :image, multiple: false
recipe_photo.js.coffee(在这里我只能得到文件名,不能得到record_id)
$ ->
$('.new_recipe_photo').fileupload
dataType: "script"
done: (e, data) ->
console.log data.files[0], data.files[0].name
recipe_photo.rb
class RecipePhoto < ActiveRecord::Base
attr_accessible :recipe_id, :image
belongs_to :recipe
mount_uploader :image, RecipeImageUploader
end