這是我發表的微柱current_user
(Devise):在微博中發佈評論爲'current_user'(Devise)的問題?
microposts_controller.rb:
def create
@user = current_user
@micropost = @user.microposts.new(params[:micropost])
@micropost.save
redirect_to @micropost
end
這是我如何張貼在一個微柱評論:
comments_controller。 RB:
def create
@micropost = Micropost.find(params[:micropost_id])
@comment = @micropost.comments.create(params[:comment])
redirect_to micropost_path(@micropost)
end
現在我想發表評論爲current_user
任何建議,爲了完成這個?
微柱/ 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 %>
編輯:
不知道,如果你需要看到這一點,但這裏是模型:
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
你是如何設定的新註釋'user'屬性?它不是attr_accessiable – yoavmatchulsky 2012-01-29 12:02:19
@yoavmatchulsky對不起,我改變了這一切。評論有一個'user_id'屬性。 – alexchenco 2012-01-29 12:05:53