使用jQuery UI选项卡,您可以使用select方法在单击选项卡时运行代码:
$( ".selector" ).tabs({
select: function(event, ui) { ... }
});
Run Code Online (Sandbox Code Playgroud)
但是代码在加载刚刚单击的选项卡之前运行.我需要在加载新标签后运行代码.我怎样才能做到这一点?谢谢阅读.
我有一个字符串,它总是至少是一个数字,但也可以在数字之前和/或之后包含字母:
"4"
"Section 2"
"4 Section"
"Section 5 Aisle"
Run Code Online (Sandbox Code Playgroud)
我需要像这样拆分字符串:
"4" becomes "4"
"Section 2" becomes "Section ","2"
"4 Aisle" becomes "4"," Aisle"
"Section 5 Aisle" becomes "Section ","5"," Aisle"
Run Code Online (Sandbox Code Playgroud)
我怎么能用Ruby 1.9.2做到这一点?
我正在使用CarrierWave(0.9.0),Fog(1.14.0)和S3来存储用户头像.确定给定用户的头像URL似乎需要很长时间.随后的通话时间大大缩短.
配置/初始化/ fog.rb
CarrierWave.configure do |config|
config.fog_credentials = {
provider: 'AWS',
aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
}
config.fog_directory = ENV['AWS_S3_BUCKET_AVATAR']
config.fog_public = false
end
Run Code Online (Sandbox Code Playgroud)
user.rb
mount_uploader :avatar, ImageUploader
Run Code Online (Sandbox Code Playgroud)
安慰
irb(main):002:0> Benchmark.measure { user.avatar_url }
=> 0.500000 0.020000 0.520000 ( 0.537884)
irb(main):003:0> Benchmark.measure { user.avatar_url }
=> 0.000000 0.000000 0.000000 ( 0.001830)
irb(main):004:0> Benchmark.measure { user.avatar_url }
=> 0.000000 0.000000 0.000000 ( 0.001454)
irb(main):005:0> Benchmark.measure { user.avatar_url }
=> 0.000000 0.000000 0.000000 ( 0.000998)
Run Code Online (Sandbox Code Playgroud)
New Relic报道有时user.avatar_url需要1秒钟.什么可能导致这么慢? …
在JSON我的Car模型的表示中,我包括一个昂贵的方法的输出:
#car.rb
def as_json(options={})
super(options.merge(methods: [:some_expensive_method]))
end
Run Code Online (Sandbox Code Playgroud)
我有一个标准的索引操作:
#cars_controller.rb
respond_to :json
def index
respond_with(Car.all)
end
Run Code Online (Sandbox Code Playgroud)
我也在JSON其他地方使用汽车的表示,如下所示:
#user_feed.rb
def feed_contents
Horse.all + Car.all
end
#user_feeds_controller.rb
respond_to :json
def index
respond_with(UserFeed.feed_contents)
end
Run Code Online (Sandbox Code Playgroud)
因为a的JSON表示car在多个地方使用,所以我希望将其car.cache_key作为自动过期的缓存键自行缓存.
这就是我目前正在做的事情:
#car.rb
def as_json(options={})
Rails.cache.fetch("#{cache_key}/as_json") do
super(options.merge(methods: [:some_expensive_method]))
end
end
Run Code Online (Sandbox Code Playgroud)
将缓存代码放入其中as_json是不正确的,因为缓存不是可as_json重复性的一部分.这样做的正确方法是什么?我正在使用Rails 3.2.15.
在可能的情况下,有一个约定可以在其实例变量上引用对象的属性.Ruby中实用的面向对象设计说:
始终将实例变量包装在访问器方法中,而不是直接引用变量...
这是一个例子,我已经解释过了:
class Gear
attr_reader :chainring, :cog
...
def ratio
# this is bad
# @chainring / @cog.to_f
# this is good
chainring / cog.to_f
end
Run Code Online (Sandbox Code Playgroud)
我看到使用实例变量创建新对象的最常见方法是:
class Book
attr_accessor :title
def initialize(title)
@title = title
end
end
Run Code Online (Sandbox Code Playgroud)
@title=直接访问实例变量title.假设我们遵循'属性over instance variable'约定,是否更适合使用self.title=,这会告诉对象自己发送消息title=,从而使用属性write方法直接对实例变量?
class Book
attr_accessor :title
def initialize(title)
self.title = title
end
end
Run Code Online (Sandbox Code Playgroud)
本书讨论了"实例变量的属性",并参考了读取实例变量,但它是否也适用于写作?
<%= some_code %>在使用Ruby on Rails时,我总是习惯将Ruby插入HTML.我刚刚注意到其他项目有时会使用<%= some_code -%>.
在Devise中,我正在以这样的方式登录我的用户:
sign_in_and_redirect(:user, user)
Run Code Online (Sandbox Code Playgroud)
在默认登录页面中,有一个用户可以选择的复选框,以便他们在返回站点时不必再次登录.但是,当您使用该sign_in_and_redirect(:user, user)行登录时,我无法确定如何将该参数设置为yes.有谁知道怎么样?谢谢阅读.
我有一些单选按钮设置如下:
<div id="typeRadios">
<input id="note_notetype_note1" name="note[notetype]" type="radio" value="note1" /><label for="note_notetype_note1">note1</label>
<input id="note_notetype_note2" name="note[notetype]" type="radio" value="note2" /><label for="note_notetype_note2">note2</label>
</div>
Run Code Online (Sandbox Code Playgroud)
我转向jQueryUI按钮,如下所示:
$("#typeRadios").buttonset();
Run Code Online (Sandbox Code Playgroud)
这是生成的HTML:
<input type="radio" value="note1" name="note[notetype]" id="note_notetype_note1" class="ui-helper-hidden-accessible">
<label for="note_notetype_note1" aria-pressed="false" class="ui-button ui-widget ui-state-default ui-button-text-only ui-corner-left" role="button" aria-disabled="false"><span class="ui-button-text">note1</span></label>
<input type="radio" value="note2" name="note[notetype]" id="note_notetype_note2" class="ui-helper-hidden-accessible">
<label for="note_notetype_note2" aria-pressed="false" class="ui-button ui-widget ui-state-default ui-button-text-only ui-corner-left" role="button" aria-disabled="false"><span class="ui-button-text">note2</span></label>
Run Code Online (Sandbox Code Playgroud)
这些按钮有效,但每当我点击一个按钮时,浏览器视口都会返回到页面顶部,就像点击<a href="#">link</a>链接时一样.
我使用的是jQuery 1.4.2和jQueryUI 1.8.7.我该如何防止这种行为?谢谢阅读.
编辑:<a href="#">link</a>部分失踪.
我正在构建一个具有相当复杂功能的Web应用程序,我正在尝试实现一种帮助用户学习使用它的方法.
我正在考虑将辅助消息附加到某些html元素上,这些元素描述了它们的功能.这些将默认显示.当用户点击消息时,它将消失并且永远不会再次显示.
我需要在数据库中使用helper_message模型来存储数据,用户拥有许多helper_messages.然后,当在页面上单击帮助器消息时,使用ajax将helper_message上的已删除字段设置为true.在页面加载时,仅加载尚未"删除"的帮助程序消息.
由于这似乎可能是一个有点常见的系统实现,有没有任何Ruby on Rails gems/jQuery插件/教程,涵盖类似的基础?
谢谢阅读.
我正在使用Backbone.js(版本0.5.3)并且在保存模型时遇到成功回调问题.即使模型已成功保存在服务器上,它也不会运行.
CoffeeScript的:
console.log 'in switch_private'
console.log "private_entry attribute is currently #{@model.get('private_entry')}"
@model.save {'private_entry': true},
success: ->
console.log 'in success'
Run Code Online (Sandbox Code Playgroud)
编译Javascript:
console.log('in switch_private');
console.log("private_entry attribute is currently " + (this.model.get('private_entry')));
return this.model.save({
'private_entry': true
}, {
success: function() {
return console.log('in success');
}
});
Run Code Online (Sandbox Code Playgroud)
控制台输出:
in switch_private
private_entry attribute is currently false
XHR finished loading: "http://localhost:3000/entries/235".
Run Code Online (Sandbox Code Playgroud)
我head :ok从Ruby on Rails的更新操作返回.
添加模型和响应参数,这样做success: (model, response) ->,并没有什么区别.出了什么问题?
编辑:根据Trevor Burnham的建议,我添加了一个错误回调,它正在运行.那么我应该从Ruby on Rails动作返回什么才能让Backbone认为保存成功呢?目前我有head :ok
编辑2:这是我更新的编译Javascript:
var this_view;
this_view = …Run Code Online (Sandbox Code Playgroud) jquery ×3
javascript ×2
jquery-ui ×2
ruby ×2
amazon-s3 ×1
backbone.js ×1
caching ×1
carrierwave ×1
coffeescript ×1
devise ×1
fog ×1
json ×1
regex ×1