添加评论模型与POST/用户模型相关联



我终于能够将用户模型与邮政模型相关联

def create
 @post = Post.new(post_params)
 @post.user = current_user

现在,我正在尝试在Post#Show页面中创建评论,但仍会收到有关我表格的错误。我遇到的两个错误是:

NoMethodError in Posts#show undefined method `comments_path' 

当我在Post#show中有@comment = Comment.new时。删除后,我得到:

ArgumentError in Posts#show First argument in form cannot contain nil or be empty

我的表格可能有什么问题?另外,如果有人可以推荐一种更好的方法来关联我的3个型号(创建评论时,基本上具有user_idpost_id,则我为建议打开)。我的评论表将出现在Post#Show页面中。我当前的代码是:

注释模型

belongs_to :user
belongs_to :post

用户模型

has_many :posts
has_many :comments

邮政模型

has_many :comments
belongs_to :user

注释控制器

class CommentsController < ApplicationController
 before_action :find_comment, only: [:show, :edit, :update, :destroy]
 def index
  @comments = Comment.all
 end
 def new
  @comment = Comment.new
 end
 def create
  @post = Post.find(params[:id])
  @comment = @post.comments.build(comment_params)
  @comment.user = current_user
  if @comment.save
   flash[:notice] = "Successfully created..."
   redirect_to comments_path
  else
   flash[:alert] = "failed"
   redirect_to root_path
  end
 end
 def show
 end
 def edit
 end
 def update
  if @comment.update
   flash[:notice] = "You updated your comment"
  else
   flash[:alert] = "Failed to update"
 end
 def destroy
  @comment.destroy
  redirect_to '/'
 end
 private
  def find_comment
   @comment = Comment.find(params[:id])
  end
  def comment_params
   params.require(:comment).permit(:comment)
  end
end

Post Controller

class PostsController < ApplicationController
  before_action :find_post, only: [:show, :edit, :update, :destroy]
  def index
    @posts = Post.all
  end
  def new
    @post = Post.new
  end
  def create
    @post = Post.new(post_params)
    @post.user = current_user
    if @post.save!
      flash[:notice] = "Successfully created..."
      redirect_to posts_path
    else
      flash[:danger] = "failed to add a post"
      render 'new'
    end
  end
  def show
  end
  def edit
  end
  def update
    if @post.update
      flash[:notice] = "Successfully updated"
      redirect_to post_path
    else
      flash[:alert] = "Failed to update Post"
      redirect_to :back
    end
  end
  def destroy
    if @post.destroy
      flash[:notice] = "Successfully delete"
      redirect_to posts_path
    else
      flash[:danger] = "Wasn't able to delete Blog post."
      redirect_to :back
    end
  end
  private
    def find_post
      @post = Post.find(params[:id])
    end
    def post_params
      params.require(:post).permit(:title, :body, :description)
    end
end

发布#show view

<%= render '/comments/form' %>

注释#表格

<%= form_for @comment do |f| %>
  <%= f.label :comment, "Title" %>
  <%= f.text_field :comment, placeholder: "Write a comment" %>
  <%= f.submit "Submit" %>
<% end %>

如果缺少一些东西,请要求我更新我的帖子。谢谢大家帮助我更好地理解我的问题。

您的关联看起来不错。错误

NoMethodError in Posts#show undefined method `comments_path'

表示您没有路由comments_pathComments#Create。基本上,当您将form_for带有Comment作为参数时,它假定您要去createupdate路由,这取决于它是新记录。最简单的事情是添加

resources :comments

到您的路线文件。但是,由于您想要与帖子关联的评论,因此您要修改路由文件具有:

resources :posts do
  resources :comments
end

然后,将您的表格更改为看起来这样:

<%= form_for [@post, @comment] do |f| %>

因此,当您提交表格时,您将有一个params[:post_id]和一个params[:id]。使用params[:post_id]查找Post。创建评论时忽略params[:id],但在更新评论时使用它。

编辑:这是有关嵌套资源的一些帮助的链接。

最新更新