0

所以基本上在我的部分我有以下代码行

...
<%= " active" if current_user."#{prop_row}" == "repeat-x" %>
...

所以我尝试使用以下变量“prop_id”,“prop_row”传递:

<%= render :partial => "users/image_props/repeat", :prop_id => "mbr", :prop_row => "main_background_repeat" %>

我得到错误

/Users/codyjames408/rails/moz/app/views/users/image_props/_repeat.html.erb:4: syntax error, unexpected tSTRING_BEG
...= ( " active" if current_user."#{prop_row}" == "repeat-x" );...
...     

                      ^

我认为错误是因为它附加了一个字符串而不是行方法。但我正在拉扯我的头发,试图弄清楚如何解决这个问题。

我很想把它变成一个大帮手方法什么的!我只是不知道怎么...

4

1 回答 1

2

如果 prop_row 是一个字符串,包含一个属性的名称,你可以这样做:

<%= " active" if current_user.attributes[prop_row] == "repeat-x" %>

或者使用这个:

<%= " active" if current_user.send(prop_row.to_sym) == "repeat-x" %>
于 2012-05-24T22:08:05.500 回答