在 model 层面是 photos belongs_to film
/*这个是 photo 的 new.html.erb */
<%= form_for [:panel, @film, @photo], :remote => true do %>
<input id="fileupload" type="file" name="photo[photo_file]" multiple>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="/jquery-file-upload/vendor/jquery.ui.widget.js"></script>
<script src="/jquery-file-upload/jquery.iframe-transport.js"></script>
<script src="/jquery-file-upload/jquery.fileupload.js"></script>
<script>
$(function () {
$('#fileupload').fileupload({
dataType: 'json',
url: '<%= panel_film_photos_path(@film.id) %>',
done: function (e, data) {
$.each(data.result, function (index, file) {
$('<li><img src="'+file.thumbnail_url+'"></li>').appendTo('#thumbs');
});
}
});
});
</script>
<ul id="thumbs" class="cf">
<!-- json-ajax response here, see fileupload script above, .appendTo css id:"thumbs"-->
</ul>
<% end %>
/*这是 photo 的 controller */
def new
@photo = Photo.new
@film = Film.find(params["film_id"])
end
def create
@photo = @film.photos.new(photo_params)
respond_to do |format|
if @photo.save
else
format.json {render json: @photo.errors, status: :unprocessable_entity}
end
end
end
我这里上传运行不成功,望大牛指点