加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 在Ruby on Rails中,渲染’shared / score’,:

发布时间:2020-12-17 03:34:40 所属栏目:百科 来源:网络整理
导读:也许我在 http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials错过了什么 似乎要渲染部分,我们可以使用 render 'shared/score' 但下一节将讨论当地人 render :partial = 'bar',:locals = { :score = @foo.score } 但是第一种形式呢?
也许我在 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 }

上面的一行不会传入当地人,为什么?

看起来似乎必须如此

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

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

解决方法

你真的想要:

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

说明

你可以在渲染上查看它的源代码.

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

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

问题在于它需要第二个参数的本地人. :locals => {:得分=> @ foo.score}起初可能看似正确,但你真的想要:{:score => @ foo.score}.

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

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

哪个没有多大意义.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读