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 に答える