我有一个观察者,我注册了一个after_commit
回调.如何判断它是在创建或更新后被触发的?我可以通过询问来判断某个项目已被销毁item.destroyed?
但是#new_record?
因为该项目已保存而无法正常工作.
我打算通过添加after_create
/ after_update
并执行类似于@action = :create
内部的操作并检查@action
at 来解决它after_commit
,但似乎观察者实例是单例,我可能只是在它到达之前覆盖一个值after_commit
.所以我以一种更丑陋的方式解决了这个问题,根据after_create/update上的item.id将动作存储在地图中,并在after_commit上检查它的值.真的很难看
还有其他方法吗?
正如@tardate所说,transaction_include_action?
虽然它是一种私有方法,但它是一个很好的指示,并且在观察者中它应该被访问#send
.
class ProductScoreObserver < ActiveRecord::Observer
observe :product
def after_commit(product)
if product.send(:transaction_include_action?, :destroy)
...
Run Code Online (Sandbox Code Playgroud)
不幸的是,该:on
选项在观察者中不起作用.
只要确保你测试你的观察者的地狱(test_after_commit
如果你使用use_transactional_fixtures 寻找宝石)所以当你升级到新的Rails版本时,你会知道它是否仍然有效.
(测试3.2.9)
我现在使用ActiveSupport :: Concern代替Observers并after_commit :blah, on: :create
在那里工作.
我可以返回包含渲染视图的Json结果吗?
我需要它来返回提交表单的新ID以及HTML和其他一些属性.
当我需要从Json对象中的一个动作返回两个(或更多)视图结果时,这也很有用.
谢谢!
我在我的网络应用程序中使用OAuth,用户可以使用twitter登录.
我想添加"switch twitter account"按钮,它实际上会清除会话,然后打开authorize_url.
由于在我的网络应用程序中清除会话不会退出twitter,authorize_url将自动验证当前的twitter.com用户.这意味着我无法注销,除非我将用户发送到twitter.com.
是否可以使用API?实现这个的最佳方法是什么?
为什么这段代码不起作用?
b if b = true
Run Code Online (Sandbox Code Playgroud)
错误: undefined local variable or method `b'
但这样做:
if b = true
b
end
Run Code Online (Sandbox Code Playgroud)
它们不应该是一样的吗?
我想在Rails 3中捕获未知的操作错误,该错误显示开发时的"未知操作"错误以及生产中的404.html.我尝试将此rescue_from
处理程序放在我的ApplicationController上(以及实际的控制器,以防万一),但我仍然看到了丑陋的错误.
我在404上有自定义的东西,它不能是普通的.html文件.
我的路线:
match '/user/:id/:action', controller: 'users'
Run Code Online (Sandbox Code Playgroud)
我正在访问的URL: /user/elado/xxx
该rescue_from
代码:
rescue_from AbstractController::ActionNotFound, :with => :action_not_found
def action_not_found
render text: "action_not_found"
end
Run Code Online (Sandbox Code Playgroud)
浏览器中的错误:
Unknown action
The action 'xxx' could not be found for UsersController
Run Code Online (Sandbox Code Playgroud)
在控制台中:
Started GET "/user/elado/xxx" for 127.0.0.1 at 2011-09-07 19:16:27 -0700
AbstractController::ActionNotFound (The action 'xxx' could not be found for UsersController):
Run Code Online (Sandbox Code Playgroud)
也试过rescue_from ActionController::UnknownAction
.
有什么建议?谢谢!
我有两个版本的iPhone应用程序,一个用于appstore,另一个用于内部测试.他们有: - 两者的FB App ID相同(它们都与相同的后端通信) - 两个不同的捆绑ID(因此它们都可以安装在设备上) - 不同的URL后缀(空和'adhoc')
他们都在登录时正常工作,但是当我在FB管理工具的应用页面上更改了iOS Bundle ID时,它显然停止了工作:我在调用之前得到了"fb_APP_ID_SUFFIX:// authorize#error = unknown%5Ferror"作为URL fbDidNotLogin,在测试应用程序上.
如果我没有放入iOS捆绑包ID它可以工作.我是否有任何选项仍然可以保护捆绑ID并让其他应用程序正常工作?
我得到了上面配置的服务器.
这是我的deploy.rb配方的重要部分:
$:.unshift(File.expand_path('./lib', ENV['rvm_path']))
require 'rvm/capistrano'
require 'bundler/capistrano'
set :rvm_ruby_string, 'ruby-1.9.2-p290'
set :rvm_type, :system
set :bundle_flags, "--deployment"
set :default_environment, {
'PATH' => ENV['PATH'],
'RAILS_ENV' => ENV['RAILS_ENV']
}
set :stages, %w(staging production)
require 'capistrano/ext/multistage'
Run Code Online (Sandbox Code Playgroud)
cap staging deploy
按原样运行会导致错误:
* executing "cd /mnt/data-store/project/releases/shared &&
bundle install --gemfile /mnt/data-store/project/releases/shared/Gemfile
--path /mnt/data-store/project/shared/bundle --deployment --without development test"
** [out :: localhost] The --deployment flag requires a Gemfile.lock.
Please make sure you have checked your Gemfile.lock into version control
before deploying.
... rolling back ...
failed: …
Run Code Online (Sandbox Code Playgroud) 我一直在我的机器上的几台主机上收到此错误,例如:
require 'open-uri'
open('https://google.com').read
Net::OpenTimeout: execution expired
from /usr/local/opt/rbenv/versions/2.4.0/lib/ruby/2.4.0/net/http.rb:904:in `initialize'
from /usr/local/opt/rbenv/versions/2.4.0/lib/ruby/2.4.0/net/http.rb:904:in `open'
from /usr/local/opt/rbenv/versions/2.4.0/lib/ruby/2.4.0/net/http.rb:904:in `block in connect'
from /usr/local/opt/rbenv/versions/2.4.0/lib/ruby/2.4.0/timeout.rb:103:in `timeout'
from /usr/local/opt/rbenv/versions/2.4.0/lib/ruby/2.4.0/net/http.rb:902:in `connect'
from /usr/local/opt/rbenv/versions/2.4.0/lib/ruby/2.4.0/net/http.rb:887:in `do_start'
from /usr/local/opt/rbenv/versions/2.4.0/lib/ruby/2.4.0/net/http.rb:876:in `start'
from /usr/local/opt/rbenv/versions/2.4.0/lib/ruby/2.4.0/open-uri.rb:323:in `open_http'
from /usr/local/opt/rbenv/versions/2.4.0/lib/ruby/2.4.0/open-uri.rb:741:in `buffer_open'
from /usr/local/opt/rbenv/versions/2.4.0/lib/ruby/2.4.0/open-uri.rb:212:in `block in open_loop'
from /usr/local/opt/rbenv/versions/2.4.0/lib/ruby/2.4.0/open-uri.rb:210:in `catch'
from /usr/local/opt/rbenv/versions/2.4.0/lib/ruby/2.4.0/open-uri.rb:210:in `open_loop'
from /usr/local/opt/rbenv/versions/2.4.0/lib/ruby/2.4.0/open-uri.rb:151:in `open_uri'
from /usr/local/opt/rbenv/versions/2.4.0/lib/ruby/2.4.0/open-uri.rb:721:in `open'
from /usr/local/opt/rbenv/versions/2.4.0/lib/ruby/2.4.0/open-uri.rb:35:in `open'
from (irb):3
from /usr/local/opt/rbenv/versions/2.4.0/bin/irb:11:in `<main>'
Run Code Online (Sandbox Code Playgroud)
不会发生在其他人身上
open('https://twitter.com').read
Run Code Online (Sandbox Code Playgroud)
来自rbenv和macOS 10.12.3的Ruby 2.4.0.还测试了Ruby 2.0.0 - macOS上的系统版本.
当我curl
或使用Node 时,我没有得到任何错误和预期的结果request
.所以这不是被阻止的主机,我的IP也没有被阻止.
试图重新安装Ruby,重启机器,问题仍然发生.
有什么指针吗?
由于Thin/Unicorn是单线程的,您如何处理Thread.current/per-request存储?
刚刚运行了一个简单的测试 - 在一个会话中设置一个密钥,从另一个会话中读取它 - 看起来它始终从同一个地方写入/读取.但不会发生在WEBrick上.
class TestController < ApplicationController
def get
render text: Thread.current[:xxx].inspect
end
def set
Thread.current[:xxx] = 1
render text: "SET to #{Thread.current[:xxx]}"
end
end
Run Code Online (Sandbox Code Playgroud)
尝试添加config.threadsafe!
到application.rb,没有变化.
存储每个请求数据的正确方法是什么?
为什么有使用Thread.current进行存储的宝石(包括Rails本身和倾斜)?他们如何克服这个问题?
可能是Thread.current 对于每个请求是安全的,但是在请求之后不能清除,我需要自己做吗?
总结下面与@skalee和@JesseWolgamott的讨论以及我的发现 -
Thread.current取决于运行应用程序的服务器.虽然服务器可能确保在同一个Thread.current上没有同时运行两个请求,但是此哈希中的值可能不会在请求之间被清除,因此在使用情况下 - 必须将初始值设置为覆盖最后一个值.
有一些众所周知的宝石使用Thread.current,如Rails,倾斜和draper.我想如果它被禁止或不安全,他们就不会使用它.在使用散列上的任何键之前,它们似乎都设置了一个值(甚至在请求结束后将其设置回原始值).
但总的来说,Thread.current并不是每个请求存储的最佳实践.对于大多数情况,更好的设计会做,但在某些情况下,使用env
可以帮助.它可以在控制器中使用,也可以在中间件中使用,并且可以注入应用程序中的任何位置.
更新2 - 似乎现在,draper错误地使用Thread.current.请参阅https://github.com/drapergem/draper/issues/390
更新3 - 修正了漏斗错误.
我可以将动作的参数映射到其他名称吗?
我想使用保留字作为动作的参数,例如:
search?q=someQuery&in=location&for=x
Run Code Online (Sandbox Code Playgroud)
因此"in"和"for"不能用作方法的参数名称.是否有内置功能或我应该创建模型绑定器?
谢谢.
我有一个UIImage,来自任一设备的相机或从URL下载.说分辨率是800x600.
我想在UIImageView中显示一个200x150的缩放图像,我有一个合适的图像缩放代码.
所以,我只是将UIImage缩放到UIImageView frame.size
(再次,200x150).在非视网膜显示器上它看起来没问题,但在视网膜显示器上它看起来很糟糕,就像较小的图片被放大一样,因为实际的像素帧是400x300.
问题是 - 当我调整大小时,我应该手动将frame.size.width
/ height
2 乘以视网膜显示,因此它会调整为400x300,或者是否有内置功能?
由于UIImage不是设备上的资源,我没有@2x
选项.
我一直在玩积木并遇到一种奇怪的行为.这是接口/实现,它只包含一个能够执行它的块:
@interface TestClass : NSObject {
#if NS_BLOCKS_AVAILABLE
void (^blk)(void);
#endif
}
- (id)initWithBlock:(void (^)(void))block;
- (void)exec;
@end
@implementation TestClass
#if NS_BLOCKS_AVAILABLE
- (id)initWithBlock:(void (^)(void))block {
if ((self = [super init])) {
blk = Block_copy(block);
}
return self;
}
- (void)exec {
if (blk) blk();
}
- (void)dealloc {
Block_release(blk);
[super dealloc];
}
#endif
@end
Run Code Online (Sandbox Code Playgroud)
虽然常规实例化并传递常规块工作:
TestClass *test = [[TestClass alloc] initWithBlock:^{
NSLog(@"TestClass");
}];
[test exec];
[test release];
Run Code Online (Sandbox Code Playgroud)
使用参考正在创建的对象的块不会:
TestClass *test1 = [[TestClass alloc] initWithBlock:^{
NSLog(@"TestClass %@", test1);
}];
[test1 …
Run Code Online (Sandbox Code Playgroud) 当我在模拟器/设备上点击Run时,我遇到了这个问题,项目编译成功,但我看到的只是"附加到(AppName)".如果我再次运行该项目,它工作正常.
在设备上运行时也会发生同样的情况,但我收到警告消息"无法从设备读取".
有任何想法吗?
asp.net-mvc ×2
iphone ×2
ruby ×2
action ×1
build ×1
bundler ×1
c# ×1
capistrano ×1
closures ×1
facebook ×1
host-object ×1
ios ×1
json ×1
logout ×1
oauth ×1
objective-c ×1
parameters ×1
passenger ×1
resize ×1
rvm ×1
scale ×1
thin ×1
transactions ×1
twitter ×1
uiimage ×1
unicorn ×1
xcode ×1