我得到了undefined method 'social_system' for #User:0x000001052e2ad8
。我以为我会使用块中的值,任何人都可以解释我做错了吗?
# User model:
facebook_url string
google_plus_url string
# Constants 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
%>
答案 0 :(得分:0)
嗯,因为错误消息是
undefined method 'social_system' for #User:0x000001052e2ad8.
暗示“social_system”被视为字符串const,而不是变量。
尝试回显它以查看正在发送的内容。 变量 social_system
的值应为'facebook_url','google_plus_url'
回声测试:
# 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
%>