Модель
class Account < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_one :account
devise :database_authenticatable, :registerable,
:recoverable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me
end
Контроллер
class AccountsController < ApplicationController
before_filter :authenticate_user!
def index
@accounts = Account.all
end
View
p id="notice"><%= notice %></p>
<table>
<tr>
<th>Account Number</th>
<th>User Id</th>
<th>Email Address</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @accounts.each do |account| %>
</tr>
<td><%= account.Number %></td>
<td><%= account.user_id %></td>
<td><%= account.user.email %></td>
<td><%= link_to 'Show', account %></td>
<td><%= link_to 'Edit', edit_account_path(account) %></td>
<td><%= link_to 'Destroy', account, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Account', new_account_path %>
Я могу получить доступ к account.user.email из других своих действий, но я озадачен, почему я не могу получить к нему доступ, и вместо этого получить неопределенный метод 'email для nil: NilClass?
Обновление: Мне нужно было проверить нулевое значение. Исправил это, поставив следующее на мой взгляд:
<%= account.user.email if account.user %>