[英]Micropost's comments on users page (Ruby on Rails)
在用户页面上,我有很多微博,我想向每个微博添加评论表格和评论。
我有三种模型:用户,微博,评论。
user.rb
class User < ActiveRecord::Base
has_many :microposts, dependent: :destroy
has_many :comments
end
micropost.rb
class Micropost < ActiveRecord::Base
belongs_to :user
has_many :comments, dependent: :destroy
end
comment.rb
class Comment < ActiveRecord::Base
attr_accessible :comment_content
belongs_to :user
belongs_to :micropost
validates :comment_content, presence: true
validates :user_id, presence: true
validates :micropost_id, presence: true
end
comments_controller.rb
class CommentsController < ApplicationController
def create
@comment = current_user.comments.build(params[:comment])
if @comment.save
flash[:success] = "Comment created!"
redirect_to current_user
else
render 'shared/_comment_form'
end
end
end
_micropost.html.erb
<tr>
<td class="micropost">
<span class="content"><%= wrap(micropost.content) %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
<%= render 'shared/comment_form' %>
</td>
</tr>
评论表
<%= form_for(@comment) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :comment_content %>
</div>
<button class="btn" type="submit">
Create
</button>
<% end %>
每个微博都必须有自己的评论。 在我的数据库中,我有注释表
id / comment_content / user_id / micropost_id
列。
未创建评论,因为RoR无法理解此新评论属于哪个微博。 我应该怎么做才能在数据库中保存所有需要的信息?
更新
users_controller
def show
@user = User.find(params[:id])
@microposts = @user.microposts.paginate(page: params[:page])
@comment = Comment.new
end
microposts_controller
def create
@micropost = current_user.microposts.build(params[:micropost])
if @micropost.save
flash[:success] = "Micropost created!"
redirect_to current_user
else
render 'shared/_micropost_form'
end
end
解!!!
非常感谢carlosramireziii和Jon! 他们都是对的
comments_controller
def create
@micropost = Micropost.find(params[:micropost_id])
@comment = Comment.new(params[:comment])
@comment.micropost = @micropost
@comment.user = current_user
if @comment.save
flash[:success] = "Comment created!"
redirect_to current_user
else
render 'shared/_comment_form'
end
end
_micropost.html.erb
<%= render 'shared/comment_form', micropost: micropost %>
评论表
<%= form_for([micropost, @comment]) do |f| %>
routes.rb
resources :microposts do
resources :comments
end
我会在您的route.rb文件中将嵌套资源用于微博和注释:
resources :microposts do
resources :comments
end
然后,在您的注释控制器中,可以通过micropost_comments_path(@micropost)
url帮助器对其进行访问,您可以执行以下操作以在create动作中建立关联:
def create
@micropost = Micropost.find(params[:micropost_id])
@comment = Comment.new(params[:comment])
@comment.micropost = @micropost
@comment.user = current_user
if @comment.save
...
end
您可以使用merge方法减少代码行数,但是我发现这有时会降低代码的可读性,并且由于在验证错误后重新显示表单,您将需要@micropost对象,因此您也可以直接获取记录。
尝试将当前微博传递给部分评论
<%= render 'shared/comment_form', micropost: micropost %>
然后将微博添加到注释form_for
调用中
<%= form_for([micropost, @comment]) do |f| %>
确保您的路线是嵌套的
# in routes.rb
resources :microposts do
resources :comments
end
然后在CommentsController
中的微博上建立CommentsController
def create
@micropost = Micropost.find(params[:micropost_id])
@comment = @micropost.comments.build(params[:comment])
...
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.