Rails 3: getting undefined method when trying to use a variable in a block
I'm getting undefined method 'social_system' for #User:0x000001052e2ad8
. I thought i'd be using the value in the block, can anyone explain what I'm doing wrong?
# User model:
facebook_url string
google_plus_url string
# Con开发者_StackOverflowstants defined in User model:
SOCIAL_SYSTEMS = [ 'facebook_url', 'google_plus_url' ]
SOCIAL_SYSTEM_NAMES = { 'facebook_url' => 'Facebook',
'google_plus_url' => 'Google +' }
# View (going to be a helper eventually)
<% User::SOCIAL_SYSTEMS.each do |social_system|
url = @user.send(social_system)
if url %>
<p><a href="<%= url %>">
<%= User::SOCIAL_SYSTEM_NAMES[social_system] -%>
</a></p>
<% end
end
%>
Hmmm, since the error msg is
undefined method 'social_system' for #User:0x000001052e2ad8.
that implies that "social_system" is being treated as a string const, not as a variable.
Try echoing it to see what is being sent. The variable social_system
should only have values of 'facebook_url', 'google_plus_url'
Echo test:
# View (going to be a helper eventually)
<% User::SOCIAL_SYSTEMS.each do |social_system|
%>
<p>social_system = <%= social_system %>
<% # url = @user.send(social_system)
# if url %>
# <p><a href="<%= url %>">
<%= User::SOCIAL_SYSTEM_NAMES[social_system] -%>
</a></p>
<% # end
end
%>
精彩评论