我通过jquery-fileupload-rails gem 在Rails 3.2应用程序中使用blueimp jquery-file-upload.
我正在尝试在上传之前在客户端调整图像大小,但是在访问文档时遇到了问题.我的代码如下.目前上传效果很好,但图片没有调整大小.
通过jquery-file-upload调整图像大小的正确语法是什么.
(基于此和本文档的coffeescript中显示了两种方法.对我来说都不适用.)
#Coffeescript
jQuery ->
if $("#new_asset").length
$("#new_asset").fileupload
dataType: "script"
add: (e, data) ->
types = /(\.|\/)(jpe?g|png)$/i
file = data.files[0]
if types.test(file.type) || types.test(file.name)
data.context = $(tmpl("template-upload", file))
$('#progress-container').append(data.context)
jqXHR = data.submit()
$("button.cancel").click (e) ->
jqXHR.abort()
else
alert("#{file.name} is not a jpeg or png image file")
progress: (e, data) ->
if data.context
progress = parseInt(data.loaded / data.total * 100, 10)
data.context.find('.bar').css('width', progress + '%')
stop: (e, data) …Run Code Online (Sandbox Code Playgroud) ruby-on-rails blueimp jquery-file-upload jquery-fileupload-rails
是否可以覆盖一个方法并仍然回退到原始方法(假设不涉及超类)?
def User
def method
# do some original stuff
end
def method
# do some new stuff
call_the_original :method
end
end
Run Code Online (Sandbox Code Playgroud)
希望我的具体例子能让我的意思更清楚。
在 User 模型中使用 activestoragehas_one_attached :avatar添加了 setter 方法。我想在调用此 setter 时做一些事情,但我仍然希望运行原始方法。
class User
has_one_attached :avatar
# According to the source (see references) this mixes in the following setter method
def avatar=(attachable)
# do activestorage stuff
end
# I want to add some custom functions to this, before still running "do activestorage
# stuff". I could copy, paste …Run Code Online (Sandbox Code Playgroud) 帮助,我打破了我的Rails3用户模型!
我首先注意到我无法从应用程序本身更新用户.
看来我也无法从rails控制台.
User.save回报false.
User.create生成一条记录id=nil,该记录未保存到数据库中.
我花了两天时间试图解决问题所在.我已经取出了所有的CanCan和Devise验证.我已经逐行完成了代码.我很难过!
日志中没有任何东西指向我明显的方向.
我评论了User.rb中的所有内容,但没有任何乐趣.
我知道这是一个非常模糊的问题,从远处回答几乎是不可能的.
我希望有人可以提出合理的步骤来解决这个问题并试图找出正在发生的事情.
我仍然是Rails的新手,并且热衷于了解它是如何工作的.
谢谢你的想法!
更新:
过去一周我一直在研究这个问题.最终我从零开始创建了一个新的rails应用程序,系统地将旧应用程序逐个移动到新应用程序中,以尝试找出问题所在.
我已将其跟踪到我的自定义Devise路线.
的routes.rb
devise_for :users, :path_prefix => 'registration', :controllers => {:registrations => 'users/registrations'}
Run Code Online (Sandbox Code Playgroud)
如果我删除:controllers行,那么我可以使用Devise用户编辑表单更新用户名.
然而,这不是一个可行的解决方案.设计编辑表单仅提供名称和电子邮件字段,但我的用户模型中还有其他几个用户需要访问的列.因此自定义路由.
所以到目前为止我已经跟踪了这个问题,但我不确定在哪里看.我尝试将registrations_controller编辑为默认值,但没有运气.(我甚至直接从设计git存储库粘贴代码).
def edit
super
end
def update
super
end
Run Code Online (Sandbox Code Playgroud)
所以我认为问题不在于控制器.
这留下了形式本身.表格定义为
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put, :multipart => true }) do |f| %>
Run Code Online (Sandbox Code Playgroud)
我看不出有任何问题.我想不出还有什么可以调试这个.
回顾一下,编辑表单按预期显示,用户可以编辑信息,单击更新,按预期重定向,不显示错误,但不保存编辑的信息.尾部显示没有调用SQL保存.
很难过,我很欣赏任何狂野的想法或建议!
非常感谢
在Rails 3.2应用程序中,我有一个coffeescript函数,可以在单击链接时切换css类.
#coffeescript
jQuery ->
$(".toggle-link").click ->
$(this).toggleClass "selected"
#view
<%= link_to "toggle", my_path, class: "toggle-link" %>
Run Code Online (Sandbox Code Playgroud)
这很好用.
但是如果我将链接移动到ajaxified部分,例如用于分页,则jquery切换停止工作.
为什么是这样?
它怎么能修复?
例如,我有 array = [1,2,3,4,5]
我想用索引4更改元素的值.
我可以通过多个步骤:
> array[4] = 'new value'
=> "new value"
> array
=> [1,2,3,4, "new value"]
Run Code Online (Sandbox Code Playgroud)
但有没有办法在线进行此操作?就像是
array.map! { |x| x == 5 ? "new value" : x }
Run Code Online (Sandbox Code Playgroud)
但使用索引号而不是值.
我已经四处搜索并阅读了ruby-docs,但我不确定我应该寻找什么!
我有一个json url,它以格式返回数据
{
"photos" : [
{
"id": 1, "image":"https://path/to/my/image/1.jpg"
},
{
"id": 2, "image":"https://path/to/my/image/2.jpg"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我在javascript函数中使用json,需要操作它来删除根密钥.即我想要的东西看起来像
[
{
"id": 1, "image":"https://path/to/my/image/1.jpg"
},
{
"id": 2, "image":"https://path/to/my/image/2.jpg"
}
]
Run Code Online (Sandbox Code Playgroud)
我一直在用各种方法进行攻击,并在SO上引用了几个类似的帖子,但似乎没有任何效果.以下似乎应该如此.
var url = 'http://path/to/my/json/feed.json';
var jsonSource = $.getJSON( url );
var jsonParsed = $.parseJSON(jsonSource);
var jsonFeed = jsonParsed.photos
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我正在尝试通过 API 访问维基百科页面上的图像和摘录。
根据文档,这样做的方法是添加prop=images到请求 URL。
api.php?action=query&titles=Albert%20Einstein&prop=images
Run Code Online (Sandbox Code Playgroud)
但是如果我结合提取物和图像
api.php?action=query&titles=Albert%20Einstein&prop=images&extracts
Run Code Online (Sandbox Code Playgroud)
我得到以下回复
parsed_response={"warnings"=>{"main"=>{"*"=>"Unrecognized parameter: 'images'"}}
Run Code Online (Sandbox Code Playgroud)
这些中的每一个都单独工作。结合其他道具(例如,extracts&exintro&explaintext&redirects)也有效。
为什么我不能通过单个 API 请求获取提取物和图像?
在模型规范中,我想测试某些方法是否被正确调用.
#models/object.rb
class Object < ActiveRecord::Base
after_validation :do_this
after_save :enqueue_that
def do_this
# does some stuff, the results of which I don't want to test
end
def enqueue_that
MyWorker.perform_later id
end
end
#spec/models/object.rb
describe Object
describe '#do_this' do
it 'is called on save with passing validations' do
object.save
expect(object).to receive(:do_this)
end
end
describe '#enqueue_that' do
it 'is called after save' do
object.save
expect(MyWorker).to receive(:perform_later).once
end
end
end
Run Code Online (Sandbox Code Playgroud)
测试失败,如下所示
Failure/Error: expect(object).to receive(:do_this).once
(#<Object:0x007fd2101c7160>).do_this(*(any args))
expected: 1 time with any arguments
received: …Run Code Online (Sandbox Code Playgroud) 以下rspec测试失败,但是与失败测试一起提供的消息似乎是预期的结果.
describe '#validate_maximum_pending_actions_not_been_reached' do
let(:action) { build :action, status: 'pending' }
before :each do
10.times do
create :action, status: 'pending'
end
end
it 'does not save the 11th action' do
expect(action.save).to raise_error #(ActiveRecord::RecordInvalid,'Validation failed: maximum number of pending actions already reached')
end
end
Failure/Error: let(:action) { build :action, status: 'pending' }
ActiveRecord::RecordInvalid:
Validation failed: maximum number of pending actions already reached
Run Code Online (Sandbox Code Playgroud)
我通过省略错误消息尝试不那么具体,只是期望测试会raise_error.测试仍然失败,但是给出了上面的描述,表明确实出现了错误.
我究竟做错了什么?
正在测试的方法如下:
validate :validate_maximum_pending_actions_not_been_reached
def validate_maximum_pending_actions_not_been_reached
errors[:base] << "maximum number of pending actions already reached" unless …Run Code Online (Sandbox Code Playgroud) 我有一个create动作,如果记录成功保存,则调用ActiveJob。
def create
@object = Object.new(importer_params)
respond_to do |format|
if @object.save
MyJob.perform_later( @object.id )
format.html { redirect_to @object, notice: t('.notice') }
else
format.html { render :new }
end
end
end
Run Code Online (Sandbox Code Playgroud)
我想测试控制器规范中正确调用了Job。
describe "POST #create" do
it {
expect {
post :create, { object: valid_attributes }
}.to change(Object, :count).by(1)
}
it {
expect {
post :create, { object: valid_attributes }
}.to have_enqueued_job(MyJob)
}
end
Run Code Online (Sandbox Code Playgroud)
但是我明白了
Failure/Error:
expect {
post :create, { object: valid_attributes }
}.to have_enqueued_job(MyJob)
expected to enqueue exactly 1 …Run Code Online (Sandbox Code Playgroud) rspec ×3
jquery ×2
ajax ×1
blueimp ×1
coffeescript ×1
javascript ×1
json ×1
mediawiki ×1
ruby ×1
wikipedia ×1