[英]Micropost's comments on users page (Ruby on Rails)
On user's page i have many microposts and i want to add comment form and comments to each micropost. 在用户页面上,我有很多微博,我想向每个微博添加评论表格和评论。
I have three models: User, Micropost, Comment. 我有三种模型:用户,微博,评论。
user.rb user.rb
class User < ActiveRecord::Base
has_many :microposts, dependent: :destroy
has_many :comments
end
micropost.rb micropost.rb
class Micropost < ActiveRecord::Base
belongs_to :user
has_many :comments, dependent: :destroy
end
comment.rb 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 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 _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>
Comment form 评论表
<%= 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 %>
Every micropost must have its own comments. 每个微博都必须有自己的评论。 In my DB i have comment table with
在我的数据库中,我有注释表
id / comment_content / user_id / micropost_id
id / comment_content / user_id / micropost_id
columns. 列。
Comment is not creating because RoR can't understand to which micropost belongs this new comment. 未创建评论,因为RoR无法理解此新评论属于哪个微博。 What should i do to have all needed information in my DB?
我应该怎么做才能在数据库中保存所有需要的信息?
UPDATE 更新
users_controller users_controller
def show
@user = User.find(params[:id])
@microposts = @user.microposts.paginate(page: params[:page])
@comment = Comment.new
end
microposts_controller 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
SOLUTION!!! 解!!!
Big thanks to carlosramireziii and Jon! 非常感谢carlosramireziii和Jon! They are both right
他们都是对的
comments_controller 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 _micropost.html.erb
<%= render 'shared/comment_form', micropost: micropost %>
Comment form 评论表
<%= form_for([micropost, @comment]) do |f| %>
routes.rb routes.rb
resources :microposts do
resources :comments
end
I would use nested resources for micropost and comments like this in your routes.rb file: 我会在您的route.rb文件中将嵌套资源用于微博和注释:
resources :microposts do
resources :comments
end
Then in your comments controller, which you access through the micropost_comments_path(@micropost)
url helper you can do the following to build the association in the create action: 然后,在您的注释控制器中,可以通过
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
You could reduce the number of lines of code using the merge method, but I find that this sometimes reduces the readability of your code, and since you'll need the @micropost object if redisplaying the form after validation errors you may as well just fetch the record. 您可以使用merge方法减少代码行数,但是我发现这有时会降低代码的可读性,并且由于在验证错误后重新显示表单,您将需要@micropost对象,因此您也可以直接获取记录。
Try passing in the current micropost to the comment partial 尝试将当前微博传递给部分评论
<%= render 'shared/comment_form', micropost: micropost %>
Then add the micropost to the comment form_for
call 然后将微博添加到注释
form_for
调用中
<%= form_for([micropost, @comment]) do |f| %>
Make sure your routes are nested 确保您的路线是嵌套的
# in routes.rb
resources :microposts do
resources :comments
end
Then build the comment off of the micropost in the CommentsController
然后在
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.