简体   繁体   English

Micropost在用户页面上的评论(Ruby on Rails)

[英]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.

相关问题 基于Ajax的micropost的评论(Ruby on Rails) - Ajax based micropost's comments (Ruby on Rails) 在Rails教程中删除ruby中的micropost - deleting micropost in ruby on rails tutorial Ruby on Rails 教程 (Michael Hartl) 第 2 章练习 2.3.3.1 “编辑用户显示页面以显示用户第一个微博的内容。” - Ruby on Rails Tutorial (Michael Hartl) Chapter 2 Exercise 2.3.3.1 “Edit the user show page to display the content of the user’s first micropost.” 基于AJAX的评论不会发布正确的微博(Ruby on Rails) - AJAX based comments don't post under correct micropost (Ruby on Rails) 如何从Hartl的Ruby on Rails教程中扩展微帖子模型? - How can I expand the micropost model from Hartl's Ruby on Rails tutorial? 在 Hartl 的 ruby-on-rails 教程的第 11 章中删除 Micropost 时出现“没有路线匹配”错误 - 完全被难住了 - 'No route matches' Error on Delete of Micropost in Chapter 11 of Hartl's ruby-on-rails tutorial - completely stumped Ruby on Rails中nil的未定义方法`save&#39;:@ micropost.save的NilClass - Undefined method `save' for nil:NilClass for @micropost.save in Ruby on Rails Ruby on Rails-Hartl教程-Micropost始终检测为空白,但不是 - Ruby on rails - Hartl tutorial - Micropost always detected as blank but they are NOT Easy Ruby on Rails问题 - 如何将注释附加到用户和文章? - Easy Ruby on Rails question — how to attach comments to both users and articles? 在RAILS中使用AJAX销毁微信时,如何重新加载当前页面 - How to reload current page when destroy a micropost with AJAX in RAILS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM