我發現了一些關於這個的帖子,但是我沒有嘗試過使用它,但迄今爲止我已經完成了它。對於rails我還是很新鮮 - 基本上我已經使用HTML和CSS,但是我接受了Skillshare Rails類,並且正在將它與Railstutorial書籍結合起來。所以請溫柔。在rails中顯示相關的數據(user_id,email等)
我有一個基本的應用程序,用戶可以創建'項目'。我用腳手架來取得'物品'。他們可能是微博。但隨着腳手架創建的觀點,我想顯示用戶的電子郵件地址,而不是電子郵件地址。我會在模型,視圖和控制器中更改哪些內容?這是我得到的。
控制器:
def email
@email = @item.user_id.email
end
視圖:
<td><%= item.content %></td>
<td><%= @email %></td>
<td><%= link_to 'Show', item %></td>
<td><%= link_to 'Edit', edit_item_path(item) %></td>
<td><%= link_to 'Destroy', item, confirm: 'Are you sure?', method: :delete %></td>
產品型號:
class Item < ActiveRecord::Base
attr_accessible :content, :user_id
validates :content, :length => { :maximum => 140 }
belongs_to :user
end
用戶模型:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
has_many :items
end
我覺得你想'@email = @ item.user.email',因爲你不能得到'user_id'的電子郵件 – 2013-02-23 02:18:59