Rails每个都为nil做了未定义的方法`name':NilClass

时间:2013-09-23 02:11:47

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

您好我已经检查过并且没有CSS文本转换:大写,在所有我的HTML或CSS文档中...但是当我使用这个“每个循环”时,我得到了国家名称的大写..而且只是以国家的名义..

我已经看了很多问题,但是我找不到任何人的类似问题,我会变成大写。

编辑:我只是清除缓存并重新加载,实际上没有大写,有A ERROR

nil的未定义方法`name':NilClass

 <% @player.citizens.each do |citizen| %>

          <%= t('generales.citizen') %> :
          <%= citizen.country.name %>

    <% end %>

这里有一些模型,

Country.rb

class Country < ActiveRecord::Base
  attr_accessible :iso, :name, :printable_name, :iso3, :numcode
end

Citizen.rb

class Citizen < ActiveRecord::Base
  attr_accessible :name, :country_id

  belongs_to :player
  belongs_to :country

end

Club.rb

class Club < ActiveRecord::Base
  attr_accessible :name, :division, :from, :to, :country_id

  belongs_to :player
  belongs_to :country

  DIVISION = %w{
    first_division
    second_division
    third_division
    amateur_division
  }

  YEARS = (1950..(Time.now.strftime('%Y')).to_i).to_a

end

好的,这段代码工作正常(俱乐部)

 <% @player.clubs.each do |club| %>
  <% if club.name.present? %>
    <p>
      <%= t 'activerecord.attributes.club.name' %> :
      <%= club.name %><br />
      <%= t 'activerecord.attributes.club.country' %> :
      <%= club.country.name %><br />
      <%= t 'activerecord.attributes.club.division' %> :
      <%= t "generales.#{club.division}" if club.division.present? %><br />
      <%= t 'activerecord.attributes.club.from' %>
      <%= club.from %>
      <%= t 'activerecord.attributes.club.to' %>
      <%= club.to %>
    </p>
  <% end %>
<% end %>

1 个答案:

答案 0 :(得分:1)

你可以做到

<% @player.citizens.each do |citizen| %>

      <%= t('generales.citizen') %> :
      <%= citizen.country.name.titleize  # This Will Make The First Letter Of Each Word A Cap %>
      <%= citizen.country.name.downcase # this will make every letter lowercase %>
<% end %>