Reputation: 31
I have 2 previously defined strings :
table_string that holds the table name and field_string that holds the field name in this table , these 2 strings are variables and change depending on the previous page where they are coming
for example in a specified page i will press on a link that will redirect me to this page with table_string=user and field_string=fullname
I should here access the attribute @user.fullname
I tried to concatenate table_string + "." + field_stringin another string let's say x and then display
<%= @x %>as if im typing
@user.fullname` but this does not work
How can I display this variable field knowing that the table name will also changes ???
Upvotes: 0
Views: 66
Reputation: 7044
Try the 'instance_variable_get' method.
table_name = 'user'
column_name = 'name'
instance_variable_get("@#{table_name}").send(column_name)
or just
instance_variable_get("@#{table_name}")[column_name]
Upvotes: 1