【问题标题】:In Ruby on Rails, render 'shared/score', :locals => { :score => @foo.score } will not pass in the local?在 Ruby on Rails 中,渲染 'shared/score', :locals => { :score => @foo.score } 不会传入本地?
【发布时间】:2011-04-12 16:52:03
【问题描述】:

也许我错过了http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials 中的某些内容

似乎要渲染一个部分,我们可以使用

render 'shared/score'

但下一节将讨论本地人

render :partial => 'bar', :locals => { :score => @foo.score }

但是第一种形式呢?

render 'shared/score', :locals => { :score => @foo.score }

上面的行在locals中不会通过,为什么?

好像是这样的

render :partial => 'shared/score', :locals => { :score => @foo.score }

但这是为什么呢? (我使用的是 Rails 3.0.6)

【问题讨论】:

  • render 'shared/score', { :score => @foo.score } 工作吗?

标签: ruby-on-rails ruby-on-rails-3


【解决方案1】:

你真的想要:

render 'shared/score', { :score => @foo.score }

说明

您可以在render 上查看他们的源代码。

http://api.rubyonrails.org/classes/ActionView/Rendering.html#method-i-render

如果您看到如果第一个参数不是哈希,它将默认为部分的名称,并将第二个参数作为本地参数传递。

关键是它想要第二个参数中的本地人。 :locals => {:score => @foo.score} 乍一看可能是对的,但你实际上想要{:score=> @foo.score}

这样做的原因是它将_render_partial 方法的:locals 选项设置为第二个参数。所以如果你按照自己的方式去做,它实际上看起来像:

:locals => {:locals => {:score=>@foo.score}}

这没有多大意义。

【讨论】:

    猜你喜欢
    • 2017-05-07
    • 1970-01-01
    • 1970-01-01
    • 2013-06-24
    • 2010-11-10
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    相关资源
    最近更新 更多