小编tim*_*one的帖子

使用 Dalli 和 memcached 获取缓存值

我正在尝试使用 Dalli 与 Rails 应用程序中的缓存存储进行交互。我只是在做片段缓存,并像这样设置了我的开发配置:

#config.action_controller.perform_caching = true
config.cache_store = :dalli_store
Run Code Online (Sandbox Code Playgroud)

这样它就不会缓存所有内容。我对缓存感兴趣的片段是这样的:

<% cache("home_main", :expires_in => 1.minute) do %>
<div class='tabbed-content content-1'>
  <%=render :partial => 'shared/launch-main', :locals => { :locations => @locations } %>
</div>
<% end %>
Run Code Online (Sandbox Code Playgroud)

当我进去跑

ruby-1.9.2-p290 :019 > Rails.cache.stats
ruby-1.9.2-p290 :019 > y _
Run Code Online (Sandbox Code Playgroud)

我得到:

conn_yields: '0'
 bytes: '17153'
 curr_items: '1'
 total_items: '5'
 evictions: '0'
 reclaimed: '0'
Run Code Online (Sandbox Code Playgroud)

现在有没有办法取回片段缓存?还是去看?我试过:

Rails.cache.fetch('home_main')
Run Code Online (Sandbox Code Playgroud)

但这不起作用。有没有办法让我看到缓存键列表?

谢谢

编辑 #1

对于@ cpuguy83 - 很明显,以 196 毫秒的时间进入数据库 :-(

Read fragment views/home_main (2.4ms)
Rendered index/homepage_launch_main.html.erb …
Run Code Online (Sandbox Code Playgroud)

memcached ruby-on-rails dalli

4
推荐指数
1
解决办法
5054
查看次数

ruby 正则表达式,看看是否只有数字

我想要一个 reqex 来检查每个字符是否是一个数字 [0-9]+。我试过了:

'4th'=~/[\d]+/
'4th'=~/\d+/
Run Code Online (Sandbox Code Playgroud)

但不工作。我将如何检查这个?

谢谢

ruby regex

4
推荐指数
2
解决办法
2508
查看次数

是否可以在一个区块内调用收益率?

我感兴趣是否可以这样做以及语法是什么.我在:

def say_it
  puts "before"
  yield("something here")
  puts "after"
end

say_it do |val|
  puts "here is " + val
  yield("other things") # ???
end 
Run Code Online (Sandbox Code Playgroud)

想想可能没有,但是如果块被转换为Proc?

thx提前

ruby

4
推荐指数
1
解决办法
101
查看次数

不知道为什么会在这里使用Proc - 不要简单

我理解proc的概念,但有时我看到这样的代码(从rails指南中获取验证http://guides.rubyonrails.org/active_record_validations_callbacks.html#using-if-and-unless-with-a-proc):

class Order < ActiveRecord::Base
  before_save :normalize_card_number,
    :if => Proc.new { |order| order.paid_with_card? }
end
Run Code Online (Sandbox Code Playgroud)

看来这可以更简单地写成:

class Order < ActiveRecord::Base
  before_save :normalize_card_number, :if => :paid_with_card? 
end
Run Code Online (Sandbox Code Playgroud)

我在这里没有了解使用Proc的优势?

thx提前

ruby ruby-on-rails proc

4
推荐指数
1
解决办法
62
查看次数

NSLayoutConstraint 并分配一个 int 变量

我觉得这可能是一个非常简单的问题,因为我刚刚开始使用 Swift,但对这种行为有点困惑。

我有一个如下所示的 NSLayoutConstraint:

let verticalConstraint = NSLayoutConstraint(item: newView, 
         attribute: NSLayoutAttribute.CenterY, 
        relatedBy: NSLayoutRelation.Equal, 
        toItem: view, 
        attribute: NSLayoutAttribute.CenterY, 
        multiplier: 1, 
        constant: 0)
Run Code Online (Sandbox Code Playgroud)

并且工作正常。当我把它改成

class ViewController: UIViewController {

  let newView = UIView()
  var verticalStartingPoint = 0

  override func viewDidLoad() {
    super.viewDidLoad()
    newView.backgroundColor = UIColor.blueColor()
    newView.setTranslatesAutoresizingMaskIntoConstraints(false)
    view.addSubview(newView)

    // not the part that is problem
    let horizontalConstraint = NSLayoutConstraint(item: newView, 
         attribute: NSLayoutAttribute.CenterX, 
         relatedBy: NSLayoutRelation.Equal, 
         toItem: view, 
         attribute: NSLayoutAttribute.CenterX, 
         multiplier: 1, 
         constant: 0)
    view.addConstraint(horizontalConstraint)


    var verticalConstraint = NSLayoutConstraint(item: newView, 
        attribute: NSLayoutAttribute.CenterY, 
       relatedBy: NSLayoutRelation.Equal, 
      toItem: view, …
Run Code Online (Sandbox Code Playgroud)

ios nslayoutconstraint swift

4
推荐指数
1
解决办法
5183
查看次数

如何在 Active Record 模型上存储临时属性

我想在对象上存储一些调试消息,因为它可以进行处理,但我不想将其存储到数据库中。这样的事情是否是一种合理的方式,将实例变量包装在具有 nil 保护的方法中?或者有更好的方法/模式吗?

class Bid < ApplicationRecord
  ...
  def debug_reasons
     @debug_reasons ||= []
  end
  ...
Run Code Online (Sandbox Code Playgroud)

进而

bid.debug_reasons << "here is a reason"
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails

4
推荐指数
1
解决办法
1792
查看次数

将非Zend_Form变量传递给ViewScript

我正在使用Zend_Form和ViewScript装饰器.这个表单将用于管理两个相当简单的对象类型,但是它是一个很大的形式,所以我想要一个表单/处理函数.

所以我有这个:

class GameManagementForm extends Zend_Form{

public function __construct($type='flash'){
    parent::__construct();

   //and later
    $this->setDecorators( array( array('ViewScript', array('viewScript' => 'game/game-management.phtml'))));
Run Code Online (Sandbox Code Playgroud)

我想要做的是能够将非Zend_Form变量传递给viewScript.我尝试将引用传递给$ this但没有运气.有没有办法调用这样的东西:

$this->setDecorators( array( array('ViewScript', array('viewScript' => 'game/game-management.phtml', $arg))));
Run Code Online (Sandbox Code Playgroud)

感谢帮助

zend-framework zend-form

3
推荐指数
1
解决办法
1535
查看次数

capistrano部署在git ls-remote调用上请求USERNAME/PASSWORD

第一次使用Capistrano用户,我已经设置了最好的部分,但是当我运行cap deploy时,我得到以下要求我的github用户名

Fri Dec 02$ cap deploy
  * executing `deploy'
  * executing `deploy:update'
 ** transaction: start
  * executing `deploy:update_code'
    executing locally: "git ls-remote https://github.com/uname/repo HEAD"
Username: 
Run Code Online (Sandbox Code Playgroud)

我假设这在我的OS X笔记本电脑上本地运行.如果我跑git ls-remote https://github.com/uname/repo,我会收到提示您输入USERNAME.但是,我可以运行git clone这个存储库,让我觉得本地ssh密钥设置正确.我在这里错过了什么?

谢谢

编辑#1 - deploy.rb的顶部

set :application, "test-rails"
set :repository,  "https://github.com/uname/app"

set :scm, :git
set :user, "deploy"
set :deploy_to, "/data/sites/site.com/apps/#{application}"
set :use_sudo, false
set :keep_releases, 5

role :web, "173.230.xxx.xxx"
Run Code Online (Sandbox Code Playgroud)

git capistrano ruby-on-rails github ruby-on-rails-3

3
推荐指数
1
解决办法
3379
查看次数

模型上的rspec和undefined方法

我正在尝试跟踪Integration Testing上的railscast并正在测试密码重置URL.我遇到NoMethodError问题而且不确定是怎么回事.

我有一个具有forgot_password值的用户模型,该值只是一个将通过电子邮件发送出去的哈希值.

这似乎有效

ruby-1.9.2-p290 :001 > u=User.find(1)
...
ruby-1.9.2-p290 :002 > u.forgot_password
 => "1280aceaf23f185a2d09a57b5534e7e3"
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

1) PasswordResets emails user when requesting password reset
   Failure/Error: click_button "jt"
   NoMethodError:
     undefined method `forgot_password=' for #<User:0x007ff916b03da0>
   # /Users/jt/rails-1/app/controllers/users_controller.rb:61:in `forgot_password_confirmation'
   # (eval):2:in `click_button'
Run Code Online (Sandbox Code Playgroud)

这是规格:

需要'spec_helper'

describe "PasswordResets" do
  it "emails user when requesting password reset" do
    user=Factory(:user)
    visit log_in_path
    click_link "password"
    fill_in "email", :with => user.email
    click_button "jt"
  end
end
Run Code Online (Sandbox Code Playgroud)

NoMethodError告诉我什么?看起来好像来自rails c,forget_password值存在.我错过了什么?

谢谢

rspec ruby-on-rails ruby-on-rails-3

3
推荐指数
1
解决办法
1878
查看次数

用于特定li的jQuery选择器

我有以下标记

<div id="search-query-autocomplete">
  <ul id="autocomplete-results">
    <li class="auto-li">blue</li>
  </ul>
</div>
Run Code Online (Sandbox Code Playgroud)

并且想要在用户按键时向第一个auto-li添加一个类.

我有以下javascript:

document.onkeydown = checkKey;
 function checkKey(e){
 e = e || window.event;

 if(e.keyCode=='40')
 {
   $('#autocomplete-results li:nth-child(0)').text('not working');
   console.log('here is 40'); //code
 }
}
Run Code Online (Sandbox Code Playgroud)

但它不起作用 - 我需要改变什么?

thx提前

jquery

3
推荐指数
1
解决办法
2160
查看次数