I created a Micropost model that have the following attributes:
<Micropost id: 1, content: "test", user_id: 1, created_at: "2012-01-25 15:34:30", updated_at: "2012-01-29 11:07:53", title: "asdasdad">
an User model with the following attributes:
<User id: 1, email: "alex@gmail.com", username: nil, etc...>
and a Comment model with the following attributes:
<Comment id: 1, content: "sdf", micropost_id: 1, user_id: nil, created_at: "2012-01-29 11:10:42", updated_at: "2012-01-29 11:10:42">
So far, I’ve only accomplished this:
- Display the username of the author of a micropost
-
Display the ID of the authors of the microposts’ comments
<h2>Micropost Index</h2> <% @microposts.each do |micropost| %> <%= micropost.title %></td> <%= micropost.content %></td> <%= link_to 'Show', micropost %></td> <%= link_to 'Edit', edit_micropost_path(micropost) %></td> <%= link_to 'Destroy', micropost, confirm: 'Are you sure?', method: :delete %><h2>Comments</h2> <% @micropost.comments.each do |comment| %> <p> <b>Comment:</b> <%= comment.content %> </p> <p> <b>Commenter</b> <%= comment.user_id %> </p> <% end %> -
I don’t know how to create a link to the authors profile (e.g. mysite.com/users/1).
- I don’t how to retrieve the name of the author of a comment and the link to his/her profile
EDIT:
Models:
user.rb:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :username
has_many :microposts
has_many :comments
def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
data = access_token.extra.raw_info
if user = User.where(:email => data.email).first
user
else # Create a user with a stub password.
User.create!(:email => data.email, :password => Devise.friendly_token[0,20])
end
end
end
micropost.rb:
class Micropost < ActiveRecord::Base
attr_accessible :title, :content
belongs_to :user
has_many :comments
end
comment.rb:
class Comment < ActiveRecord::Base
attr_accessible :content, :user_id
belongs_to :micropost
belongs_to :user
end
Micropost controller:
controllers/microposts.rb
def show
@micropost = Micropost.find(params[:id])
end
def new
@micropost = Micropost.new
end
def create
@user = current_user
@micropost = @user.microposts.new(params[:micropost])
@micropost.save
redirect_to @micropost
end
Any suggestions to accomplish this?
To make a link to the user you can use
In general part of the “Rails Magic” is, that you if you set up an association correctly, that you can access the related objects through the dot notation. That means, you don’t need to say
comment.user_idbut instead go directly for the associated user object, e.g.comment.user.usernameorcomment.user.email… you get the idea 🙂In order to to this you should have set up your models like this: