Rails 4 - 将变量传递给部分 [英] Rails 4 - passing variable to partial

查看:28
本文介绍了Rails 4 - 将变量传递给部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 Ruby on Rails 教程,但在尝试将变量传递给部分时遇到了问题.

I am following the Ruby on Rails tutorial and have come across a problem while trying to pass variables to partials.

我的_user部分如下

<li>
  <%= gravatar_for user, size: 52 %>
  <%= link_to user.name, user %>
</li>

我想为大小值传入一个数字.我正在尝试如下但没有任何运气.

I would like to pass in a number for the size value. I am trying as follows without any luck.

<%= render @users, :locals => {:size => 30} %>

推荐答案

如果您要传递局部变量,则需要完整的渲染部分语法

You need the full render partial syntax if you are passing locals

<%= render @users, :locals => {:size => 30} %>

成为

<%= render :partial => 'users', :collection => @users, :locals => {:size => 30} %>

或者使用新的哈希语法

<%= render partial: 'users', collection: @users, locals: {size: 30} %>

我认为这更具可读性

这篇关于Rails 4 - 将变量传递给部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆