This is how I post a Micropost as current_user (Devise):
microposts_controller.rb:
def create
@user = current_user
@micropost = @user.microposts.new(params[:micropost])
@micropost.save
redirect_to @micropost
end
This is how I post a comment in a micropost:
comments_controller.rb:
def create
@micropost = Micropost.find(params[:micropost_id])
@comment = @micropost.comments.create(params[:comment])
redirect_to micropost_path(@micropost)
end
Now I would like to post a comment as current_user
Any suggestions in order to accomplish this?
microposts/show.html.erb
<h2>Add a comment:</h2>
<%= form_for([@micropost, @micropost.comments.build]) do |f| %>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
EDIT:
Not sure if you need to see this but here are the models:
comment.rb:
class Comment < ActiveRecord::Base
attr_accessible :content, :user_id
belongs_to :micropost
belongs_to :user
end
micropost.rb
class Micropost < ActiveRecord::Base
attr_accessible :title, :content
belongs_to :user
has_many :comments
end
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
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
has_many :microposts
has_many :comments
end
Try this in your comments_controller:
Until now, I think your comments didn’t have a user attached to them..
Edit – I changed :user to be :user_id. makes more sense, since we don’t have a comment yet.