如何将评论表与正确的帖子关联

问题描述 投票:1回答:3

我有用户/微博/评论模型,用户可以在其中评论其他人的微博。在每个帖子下都会显示一个文本字段,以便用户可以输入评论,但是我很难找到Micropost ID。我认为问题出在我的form_for注释或控制器中,但我不确定。希望得到帮助,谢谢。

错误:如果没有ID,找不到微信

型号:

User Model: has many microposts, has many comments
Micropost Model: belongs to user, has many comments
Comment Model: belongs to micropost, belongs to user

用户控制器:

def show #(the profile page where all the posts and comments are)
  @user = User.find(params[:id])
  @microposts = @user.microposts.paginate(page: params[:page])
  @micropost  = current_user.microposts.build if signed_in?
  @comments = @micropost.comments
  @comment = current_user.comments.build(:micropost => @micropost) if signed_in?
end

注释控制器:

def create
  @micropost = Micropost.find(params[:id])
  @comment = current_user.comments.build(:micropost => @micropost) #can someone explain what happens in the parentheses? 
  @comment.user = current_user
  @comment.save
  redirect_to :back
end

查看/评论/ _评论形式:

<%= form_for(@comment) do |f| %>
  <div id="comment_field">
    <%= f.text_field :content, placeholder: "Say Something..." %>
  </div>
<% end %>

路线:

resources :users
resources :microposts, only: [:create, :destroy]
resources :comments, only: [:create, :destroy]
ruby-on-rails form-for
3个回答
3
投票

只需为micropost_id添加一个隐藏字段

<%= form_for(@comment) do |f| %>
  <%= f.hidden_field :micropost_id, value: @micropost.id %>
  <div id="comment_field">
    <%= f.text_field :content, placeholder: "Say Something..." %>
  </div>
<% end %>

更新:在不对控制器进行任何更改的情况下传递micropost_id

基于您的评论控制器,您发现基于micropostparams[:id]在提交表单时会丢失。下面的代码解决了该问题。但是,我建议您查看嵌套的资源,这些资源会使控制器代码更漂亮,更流畅。

<%= form_for @comment do |f| %>
  <%= hidden_field_tag :id, @micropost.id %>
  <div id="comment_field">
    <%= f.text_field :content, placeholder: "Say Something..." %>
  </div>
<% end %>

或更新表格的action

<%= form_for @comment, url: comments_path(id: @micropost.id) do |f| %>
  <div id="comment_field">
    <%= f.text_field :content, placeholder: "Say Something..." %>
  </div>
<% end %>

更新:带有对注释控制器的编辑

# view
<%= form_for @comment do |f| %>
  <%= hidden_field_tag :micropost_id, @micropost.id %>
  <div id="comment_field">
    <%= f.text_field :content, placeholder: "Say Something..." %>
  </div>
<% end %>

# comments_controller.rb

def create
  @micropost = Micropost.find params[:micropost_id]
  @comment = current_user.comments.build
  @comment.micropost = @micropost
  @comment.save
end

1
投票

您应该以这种方式设置您的评论资源:

resources :users
resources :microposts, only: [:create, :destroy] do
  resources :comments, only: [:create, :destroy]
end

以上资源称为嵌套资源。而且,由于您的评论始终与微博有关,因此您应该nest将评论资源放入microposts并在Comment Controller中:

def create
  @micropost = Micropost.find(params[:id])
  @comment = current_user.comments.build(:micropost => @micropost) #can someone explain what happens in the parentheses?
  @comment.save
  redirect_to :back
end

上述构建方法将创建一个新的Comment /对象模型/实例,并且您使用了current_user.comments意味着该对象将自动具有user_id = current_user.id,而无需再次指定。并且'build(:micropost => @micropost)'将micropost's id添加到@comment对象。


0
投票

我有同样的问题。...我无法在我的帖子下创建评论。以下是我的方法:CommentsController

def create
    @comment = current_user.comments.build(comment_params)
    if @comment.save
      flash[:success] = 'comment posted'
    else
      flash[:warning] = 'comment not created'
    end
    redirect_back fallback_location: @comment  
end
private
  def comment_params
    params.require(:comment).permit(:user_id, :post_id, :comment_text)
  end

comment_form.html.erb

<%= form_for(@comment) do |f| %>
    <div class="field mt-2">
        <%= f.text_area :comment_text, class: "form-control", placeholder: "Comment on this post..." %>
        <%= f.hidden_field :post_id, value: @post.id %>
    </div>
    <div class="actions">
        <%= f.submit "Create Comment", class: "form-control btn btn-success mt-1" %>
    </div>
<% end %>

post.html.erb

<% @user_posts.each do |user_post| %>
    <div class="p-3 mb-3 bg-secondary rounded col-md-8 offset-md-2 text-white">
        <small>Author: <%=  user_post.user.first_name %> <%=  user_post.user.last_name %></small><br>
        <%= user_post.post_text %>
        <% if current_user.id == user_post.user.id %>
            <%= button_to 'delete', user_post, method: :delete, data: { confirm: 'Are you sure?' }, class:'btn btn-danger btn-sm' %> 
        <% end %>
        <%= render 'comments/comment_form' %>
    </div>
    <% if !user_post.comments.empty? %>
        <% user_post.comments.each do |c| %>
        <div class="container">
            <div class="p-3 mb-3 bg-primary rounded col-md-8 offset-md-2 text-white">
                <%= c.comment_text %>
            </div>
        </div>
        <% end %>
    <% end %>
<% end %>

我在做什么错?任何帮助将是感激的

© www.soinside.com 2019 - 2024. All rights reserved.