Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplaying associated data in rails (user_id, email, etc)
    primarykey
    data
    text
    <p>I've found a few posts about this but nothing that I've tried to work with has done it for me so far. I'm still pretty fresh to rails - I'm basically solid with HTML and CSS, but I took the Skillshare Rails class and I'm working on combining that with the Railstutorial book. So please be gentle.</p> <p>I have a basic app, with users that can create 'items.' I used scaffolding to get 'items' up. They might as well be microposts. But with the views that scaffolding creates, I wanted to display the email address of the user instead of the email address. What would I change in the model, view, and controller? Here's what I've got.</p> <p>controller:</p> <pre><code>def email @email = @item.user_id.email end </code></pre> <p>view:</p> <pre><code>&lt;td&gt;&lt;%= item.content %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= @email %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= link_to 'Show', item %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= link_to 'Edit', edit_item_path(item) %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= link_to 'Destroy', item, confirm: 'Are you sure?', method: :delete %&gt;&lt;/td&gt; </code></pre> <p>item model:</p> <pre><code>class Item &lt; ActiveRecord::Base attr_accessible :content, :user_id validates :content, :length =&gt; { :maximum =&gt; 140 } belongs_to :user end </code></pre> <p>user model:</p> <pre><code>class User &lt; 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload