添加注释模型以与帖子/用户模型相关联

时间:2017-02-18 22:00:46

标签: ruby-on-rails forms

我终于能够将用户模型与帖子模型关联:

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

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

NoMethodError in Posts#show undefined method `comments_path' 

@comment = Comment.new中有Post#show的时候。当它被删除时,我得到:

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

我的表格可能有什么问题?此外,如果有人可以推荐一种更好的方式来关联我的3个模型(创建评论时基本上有一个user_id和一个post_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

后置控制器

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 %>

如果缺少某些内容,请让我更新我的帖子。谢谢所有帮助我更好地理解我的问题的人。

1 个答案:

答案 0 :(得分:1)

您的协会似乎很好。错误

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]

因此,当您提交表单时,您将拥有Postparams[:post_id]。使用params[:id]查找if。在您创建评论时忽略$newvalue = !get_option('maintenance'); update_option('maintenance', $newvalue); ,但在您更新评论时使用该评论。

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