我正在关注 Ruby on Rails 教程,并在尝试将变量传递给部分时遇到问题。
My _user
部分如下
<li>
<%= gravatar_for user, size: 52 %>
<%= link_to user.name, user %>
</li>
我想传递一个数字作为尺寸值。我尝试如下但没有任何运气。
<%= render @users, :locals => {:size => 30} %>
从 Rails api 上部分渲染:
渲染默认情况
如果您不打算使用集合或布局等任何选项,您还可以使用 render 的速记默认值来渲染部分内容。
例子:
# Instead of <%= render partial: "account" %>
<%= render "account" %>
# Instead of <%= render partial: "account", locals: { account: @buyer } %>
<%= render "account", account: @buyer %>
# @account.to_partial_path returns 'accounts/account', so it can be used to replace:
# <%= render partial: "accounts/account", locals: { account: @account} %>
<%= render @account %>
# @posts is an array of Post instances, so every post record returns 'posts/post' on `to_partial_path`,
# that's why we can replace:
# <%= render partial: "posts/post", collection: @posts %>
<%= render @posts %>
因此,您可以使用传递局部变量size
渲染如下:
<%= render @users, size: 50 %>
然后在中使用它_user.html.erb
部分的:
<li>
<%= gravatar_for user, size: size %>
<%= link_to user.name, user %>
</li>
注意size: size
相当于:size => size
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)