我正在使用carrierwave 和Jcrop 裁剪图像。保存图像的模型被调用Item
,存储图像的字段被调用cover
。
种植工作进展顺利。问题与为项目投票的 ajax 动作有关。
每个项目都有一个名为 的字段votes
。我在项目顶部有一个按钮,单击该按钮时会votes
按增量将值加 1。如果我不使用 ajax 操作,它会正确更新记录,不会造成任何伤害。但是,如果我使用 ajax 操作,它会使用新属性更新 Item 记录中的所有字段,这些属性将裁剪图像的尺寸更改为几乎为零,然后触发 after_update 回调来重新处理图像。
结果是我得到了一个 1px x 1px 并且完全白色的图像。
如何将我的 ajax 操作更改为不发生这种情况?
谢谢
def vote
item = Item.find(params[:item_id])
item.increment!(:votes)
if request.xhr?
render json: {votes: item.votes}
else
redirect_to users_url
end
end
jQuery ($) ->
$('form.vote').submit (evt) ->
evt.preventDefault()
$form = $ @
$form.find(':submit').prop 'disabled', true
$.post($form.attr('action'), $form.serializeArray(), type: 'json').done (data) ->
$form.parent().find(".vote_count").text data.votes
并作为参考项目模型:
class Item < ActiveRecord::Base
before_create :update_image_attributes
before_update :reprocess_image, :if => :cropping?
belongs_to :user
attr_accessible :available, :cover, :privacy, :size, :title, :votes, :user_id, :description, :crop_x, :crop_y, :crop_w, :crop_h
mount_uploader :cover, CoverUploader
def cropping?
!crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
end
private
def update_image_attributes
if cover.present?
self.content_type = cover.file.content_type
self.file_size = cover.file.size
self.width, self.height = `identify -format "%wx%h" #{cover.file.path}`.split(/x/)
end
end
def reprocess_image
cover.reprocess(crop_x, crop_y, crop_w, crop_h)
cover.cache_stored_file!
end
end
和控制器
class ItemsController < ApplicationController
before_filter :authenticate
def crop
@item = Item.find(params[:image_id])
@user = User.find(params[:user_id])
respond_to do |format|
if @item.update_attributes(:crop_x => params[:crop_x],:crop_y => params[:crop_y],:crop_w => params[:crop_w],:crop_h => params[:crop_h])
format.html { redirect_to items_url }
format.js
else
redirect_to root_url
end
end
end
# GET /items
# GET /items.json
def index
@items = Item.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @items }
end
end
# GET /items/1
# GET /items/1.json
def show
@item = Item.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @item }
end
end
# GET /items/new
# GET /items/new.json
def new
@item = Item.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @item }
end
end
# GET /items/1/edit
def edit
@item = Item.find(params[:id])
end
# POST /items
# POST /items.json
def create
@item = Item.create(params[:item])
respond_to do |format|
if @item.save
format.html { redirect_to items_url }
format.js
else
format.html { render action: "new" }
format.json { render json: @item.errors, status: :unprocessable_entity }
end
end
end
# PUT /items/1
# PUT /items/1.json
def update
@item = Item.find(params[:id])
respond_to do |format|
if @item.update_attributes(params[:item])
format.html { redirect_to @item, notice: 'Item was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @item.errors, status: :unprocessable_entity }
end
end
end
# DELETE /items/1
# DELETE /items/1.json
def destroy
@item = Item.find(params[:id])
@item.destroy
respond_to do |format|
format.html { redirect_to items_url }
format.json { head :no_content }
end
end
private
def authenticate
if current_user.nil?
redirect_to root_path, :alert => "You must first log in to access this page"
end
end
end
更新 - 提交日志
Started POST "/vote" for 127.0.0.1 at 2012-08-15 23:28:03 +0200
Processing by VotesController#vote as */*
Parameters: {"utf8"=>"✓", "authenticity_token"=>"uVxsVGsXImC2VPsavIjZfIUMnGmlyjYEmubI8oODgdk=", "item_id"=>"107", "user_id"=>"1"}
Category Load (0.2ms) SELECT "categories".* FROM "categories"
Color Load (0.1ms) SELECT "colors".* FROM "colors"
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", "1"]]
(0.1ms) begin transaction
SQL (9.2ms) INSERT INTO "votes" ("created_at", "item_id", "updated_at", "user_id") VALUES (?, ?, ?, ?) [["created_at", Wed, 15 Aug 2012 21:28:03 UTC +00:00], ["item_id", 107], ["updated_at", Wed, 15 Aug 2012 21:28:03 UTC +00:00], ["user_id", 1]]
(3.1ms) commit transaction
Item Load (0.3ms) SELECT "items".* FROM "items" WHERE "items"."id" = ? LIMIT 1 [["id", 107]]
(0.0ms) begin transaction
(0.4ms) UPDATE "items" SET "votes" = 1, "updated_at" = '2012-08-15 21:28:03.656131' WHERE "items"."id" = 107
(23.2ms) commit transaction