小编Kev*_*onk的帖子

将变量传递给Rails StateMachine gem转换

是否可以在转换中发送变量?即

@car.crash!(:crashed_by => current_user)
Run Code Online (Sandbox Code Playgroud)

我在我的模型中有回调,但我需要向他们发送启动过渡的用户

after_crash do |car, transition|
  # Log the car crashers name
end
Run Code Online (Sandbox Code Playgroud)

我无法访问current_user,因为我在模型中而不是Controller/View.

在你说之前......我知道我知道.

不要尝试访问模型中的会话变量

我知道了.

但是,每当您希望创建一个记录或审核某些内容的回调时,您很可能想知道是谁造成的?通常我的控制器中有一些东西像...

@foo.some_method(current_user)
Run Code Online (Sandbox Code Playgroud)

我的Foo模型会期望一些用户发起some_method但是我如何通过StateMachine gem转换呢?

ruby-on-rails state-machine

16
推荐指数
2
解决办法
6340
查看次数

为什么背景附件:固定使背景大小:封面大小调整为窗口比例?

如果您希望标题图像调整大小以覆盖整个标题但又希望修复背景附件,则背景图像将不再覆盖包含div但将尝试覆盖整个窗口.

这是一个显示问题的小提琴.只需切换CSS第13行的推荐.当您更改为 http://jsfiddle.net/TqQv7/155/时

#top-left {
    position: absolute;
    top: 0px;
    left: 0px;
    width: 50%;
    height: 50%;
    background: #000 url('http://placekitten.com/2000/1000') no-repeat;
    -moz-background-size: cover;
    background-size: cover;
    -webkit-background-size: cover;
    -o-background-size: cover;
    background-color: transparent;
    /* background-attachment: fixed; */
}
Run Code Online (Sandbox Code Playgroud)

background-attachment默认为'scroll'.因此,通过"固定"注释,猫图片大小调整为左上方框的形状没有问题但是"固定"猫背景保持固定到页面但猫图片大小则"覆盖"整个页面.

理想情况下,我想在这里重新创建标题:http://startbootstrap.com/templates/stylish-portfolio/index.html

但是标题设置为页面高度的50%.它在此示例中有效,因为标题是整页.

这似乎与标准兼容,因为所有现代浏览器看起来都是这样做但我不明白它为什么会这样做?

html css background-image css3

8
推荐指数
1
解决办法
1万
查看次数

JQuery延迟使用.each()

有关如何使用JQuery的延迟方法以及检测所有已更改的表单并将每个表单作为Ajax帖子提交的功能的任何想法?

如果我只列出一大堆表单提交,但是如果我使用...我可以得到相同的工作

$('form.changed').each(function(){
  return $(this).submitWithAjax();
});
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用的更完整版本的代码就在这里...... 在JS Fiddle

提前致谢!

jquery jquery-deferred

7
推荐指数
1
解决办法
6379
查看次数

重定向到表单验证错误的路由别名

如果我在一个路由别名,如/ register,我有一个表单错误,我渲染:new,路径是否可能/注册仍然?

目前它是渲染/新的

我可以做一个redirect_to register_path然后我会丢失params?

它使以下测试失败:

  Scenario: Try registering with a bad staff number
Given I am on the registration page
When I fill in "email" with "kevin@acme.com"
And I fill in "First Name" with "Kevin"
And I fill in "last name" with "Monk"
And I fill in "Employee Number" with "something barking123"
And I press "Register"
Then I should be on the registration page
And I should see "Your employee ID number looks incorrect."
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails cucumber

5
推荐指数
1
解决办法
705
查看次数

用拉链在红宝石中转置哈希

以下哈希是否更容易转置?我有一个有效的解决方案,但转置方法很难阅读.用例是作业的散列(j1,j2,j3等)以及它们出现的日期(d1,d2,d3等等).要求是获取按日期然后作业分组的事件的散列(e1,e2,e3等..)并将它们转置为按作业然后日期分组的事件.

我尝试使用#zip方法但是需要在j1:d1处注入nil,然后将它们从结果中删除.我也很难找到带有块参数的#zip的示例用法.我知道带块的#zip总是返回nil,但结果我无法理解你将如何实际使用它.

require 'rspec'
require 'pry'

#     |  d1  |  d2  |  d3  |
#     ----------------------
# j1  |      |  e2  |  e3  |
# --------------------------
# j2  |  e4  |  e5  |  e6  |
# --------------------------
# j3  |  e7  |      |  e9  |
# --------------------------

def transpose(h)
  Hash[
    dates(h).map do |d|
      [
        d,
        Hash[ h.keys.map do |j|
          h[j][d] ? [j, h[j][d]] : nil
        end.compact ]
      ]
    end
  ] …
Run Code Online (Sandbox Code Playgroud)

ruby hash ruby-on-rails

5
推荐指数
1
解决办法
453
查看次数

双向自引用关联

以Ryan Bates的asciicast为例:http://asciicasts.com/episodes/163-self-referential-association

他以两个User用户结束

  • :朋友
  • :inverse_friends

鉴于用户不关心是谁煽动友谊,你会想要一个简单的用户关联

  • :朋友

这包括两种关系.即,由用户发起的关系和由用户的朋友发起的关系.

那么如何实现这种双向自引用关联呢?

更新 - Josh Susser在此发表了一篇文章:http: //blog.hasmanythrough.com/2006/4/21/self-referential-through

但是,它仍然讨论has_many:sources和has_many:当真的应该有一个has_many:包含源和接收器的节点时接收.

ruby-on-rails bidirectional self-reference

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

宝石更新系统 - 捕获22个302重定向

如果我尝试在我的服务器上安装任何gem,那么我会得到302重定向,例如

gem install clickatell -V
GET http://rubygems.org/latest_specs.4.8.gz
302 Found
HEAD http://rubygems.org/specs.4.8.gz
connection reset after 2 requests, retrying
HEAD http://rubygems.org/specs.4.8.gz
302 Found
Run Code Online (Sandbox Code Playgroud)

一篇关于rubygems的文章建议做一个gem更新--system

http://help.rubygems.org/kb/rubygems/why-do-i-get-http-response-302-or-301-when-installing-a-gem

gem update --system -V
Updating RubyGems
GET 302 Found: http://gems.rubyforge.org/latest_specs.4.8.gz
connection reset after 2 requests, retrying
HEAD 302 Found: http://gems.rubyforge.org/specs.4.8.gz
connection reset after 2 requests, retrying
HEAD 302 Found: http://gems.rubyforge.org/yaml
ERROR:  http://gems.rubyforge.org/ does not appear to be a repository
ERROR:  While executing gem ... (Gem::RemoteFetcher::FetchError)
Errno::ETIMEDOUT: Connection timed out - connect(2) (http://gems.rubyforge.org/yaml)
Run Code Online (Sandbox Code Playgroud)

这当然只是宝石本身的更新.

我正在运行rubygems 1.3.1最新版本是1.6.1

有没有办法可以更新Rubygems而不会陷入我的302重定向陷阱.

问候, …

ruby gem rubygems

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

如何使用HAML:javascript过滤器向脚本添加ID属性

我试图遵循这个Rails/Backbone示例,但他们使用的是erb而不是haml.见第45-48行

https://github.com/AndrewGertig/backbone-demo/blob/master/app/views/dogs/index.html.erb

似乎不可能在脚本中添加:id属性,因此看起来像......

<script id="dog_template" type="text/html">
  <td>{{name}}</td>
  <td>{{age}}</td>
</script>
Run Code Online (Sandbox Code Playgroud)

haml backbone.js

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