Rails 3:尝试在块中使用变量时获取未定义的方法

时间:2011-10-11 02:14:38

标签: ruby-on-rails ruby-on-rails-3 methods

我得到了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
%>

1 个答案:

答案 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
%>