Ajax正在更新裁剪图像上的所有属性,因此在没有裁剪尺寸的情况下重新处理图像
我正在用carrierwave和Jcrop裁剪图像。保存图像的模型称为Ajax正在更新裁剪图像上的所有属性,因此在没有裁剪尺寸的情况下重新处理图像,ajax,activerecord,ruby-on-rails-3.2,carrierwave,jcrop,Ajax,Activerecord,Ruby On Rails 3.2,Carrierwave,Jcrop,我正在用carrierwave和Jcrop裁剪图像。保存图像的模型称为项,存储图像的字段称为封面 庄稼长得很好。问题是关于投票项目的ajax操作 每个项目都有一个名为“投票”的字段。我在项目顶部有一个按钮,当单击该按钮时,会将1按增量添加到投票值中。如果我不使用ajax操作,它会正确更新记录,不会造成任何伤害。但是,如果我使用ajax操作,它会使用新属性更新项目记录中的所有字段,这些属性会将裁剪图像的维度更改为几乎为零,然后会触发重新处理图像的after_update回调 结果是我得到的图像是1
项
,存储图像的字段称为封面
庄稼长得很好。问题是关于投票项目的ajax操作
每个项目都有一个名为“投票”的字段。我在项目顶部有一个按钮,当单击该按钮时,会将1按增量添加到投票
值中。如果我不使用ajax操作,它会正确更新记录,不会造成任何伤害。但是,如果我使用ajax操作,它会使用新属性更新项目记录中的所有字段,这些属性会将裁剪图像的维度更改为几乎为零,然后会触发重新处理图像的after_update回调
结果是我得到的图像是1px乘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
当你使用ajax时,你的日志对参数有何评论?我更新了它,日志没有说太多。顺便说一句,你可能不想在数据库中存储crop_x等。你只需要在裁剪时使用它们,这样它们就可以是普通属性(attr_accessor):裁剪?始终为true,因为您将值存储在DB中。。。
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