Ruby on rails 将评论发布为';当前用户';(设计)在一个微观世界?
这就是我将Micropost发布为当前用户的方式: microposts\u controller.rb:Ruby on rails 将评论发布为';当前用户';(设计)在一个微观世界?,ruby-on-rails,Ruby On Rails,这就是我将Micropost发布为当前用户的方式: microposts\u controller.rb: def create @user = current_user @micropost = @user.microposts.new(params[:micropost]) @micropost.save redirect_to @micropost end def create @micropost = Micropost.find(pa
def create
@user = current_user
@micropost = @user.microposts.new(params[:micropost])
@micropost.save
redirect_to @micropost
end
def create
@micropost = Micropost.find(params[:micropost_id])
@comment = @micropost.comments.create(params[:comment])
redirect_to micropost_path(@micropost)
end
class Comment < ActiveRecord::Base
attr_accessible :content, :user_id
belongs_to :micropost
belongs_to :user
end
以下是我在micropost中发表评论的方式:
评论\u controller.rb:
def create
@user = current_user
@micropost = @user.microposts.new(params[:micropost])
@micropost.save
redirect_to @micropost
end
def create
@micropost = Micropost.find(params[:micropost_id])
@comment = @micropost.comments.create(params[:comment])
redirect_to micropost_path(@micropost)
end
class Comment < ActiveRecord::Base
attr_accessible :content, :user_id
belongs_to :micropost
belongs_to :user
end
现在我想以current\u用户的身份发表评论
为了实现这一点,有什么建议吗
microspost/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 %>
class Micropost < ActiveRecord::Base
attr_accessible :title, :content
belongs_to :user
has_many :comments
end
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
添加注释:
编辑:
不确定您是否需要查看此信息,但以下是模型:
comment.rb:
def create
@user = current_user
@micropost = @user.microposts.new(params[:micropost])
@micropost.save
redirect_to @micropost
end
def create
@micropost = Micropost.find(params[:micropost_id])
@comment = @micropost.comments.create(params[:comment])
redirect_to micropost_path(@micropost)
end
class Comment < ActiveRecord::Base
attr_accessible :content, :user_id
belongs_to :micropost
belongs_to :user
end
class注释
microspost.rb
<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 %>
class Micropost < ActiveRecord::Base
attr_accessible :title, :content
belongs_to :user
has_many :comments
end
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
class Micropost
user.rb
<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 %>
class Micropost < ActiveRecord::Base
attr_accessible :title, :content
belongs_to :user
has_many :comments
end
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
class用户
在您的评论中尝试以下内容:
def create
@micropost = Micropost.find(params[:micropost_id])
comment_attr = params[:comment].merge :user_id => current_user.id
@comment = @micropost.comments.create(comment_attr)
redirect_to micropost_path(@micropost)
end
到目前为止,我认为你的评论没有附加用户
编辑-我更改了:user to be:user_id。这更有意义,因为我们还没有评论。如何在新评论中设置user
属性?它不是attr_accessiable@yoavmatchulsky对不起,我改了。注释具有user\u id
属性。感谢您的帮助,但我得到以下信息:数据库已锁定。app/controllers/comments\u controller.rb:22:in
create'。{“utf8”=>“✓", "真实性令牌“=>”ciwlksY+xXnS6zW6xwAGayK3N/5lhRwTa1pa5x9SYOg=”,“评论”=>{“内容”=>“ssadasd”},“提交”=>“创建评论”,“微博客id”=>“3”}`