新手问题 给话题添加评论功能报错

babyhai · 2017年06月02日 · 最后由 babyhai 回复于 2017年06月02日 · 1828 次阅读

我的环境:

ruby   2.3.3
Rails   5.0.2
centos  7.1

这是我的 comment(评论)controller

class CommentsController < ApplicationController
  before_action :authenticate_user!, only: [:new, :destroy]


  def new
    @user = User.find(current_user) #当前用户
    @group = @user.group.find params[:group_id]
    @comment = Group.comment.new
  end

  def create
    #@content = commentabel_record.comment.create params[:comment]
    @user = User.find(current_user) #当前用户
    @group = @user.group.find params[:group_id]

    @comment = @group.comments.build params[:comment]
    @comment.user = @user


    if @comment.save
      redirect_to commentabel_record, notice: '创建评论成功'
    else
      redirect_to commentabel_record, notice: '创建评论失败'
    end
  end

  def destroy
    @comment = commentable_record.comments.find(params[:id])
    if @comment.user == current_user or current_user.id == 1
      @comment.destroy
    end

    redirect_to commentable_record, :notice => '成功删除评论 '
  end


  protected
  def commentable_record
    Group.find(params[:group_id])
  end
end

这是我的 group(话题)的 show 页面

<div class="container">
  <div class="row">
    <div class="col-md-8">
      <div id="main-content">
        <p><%= markdown(@group.title)%></p>
        <p><%= markdown(@group.description) %></p>
      </div>
      <%= link_to '编辑', edit_group_path(@group)%>
      <%= link_to '删除', group_path(@group),
        method: :delete,
        data: {confirm: '确定删除该话题?'}
      %>
    </div>
      <!-- 显示话题评论 -->
      <%= render @group.comments %>
      <!--  评论表单-->
      <div class="comment-area">
        <%= render 'comments/form' %></br>
      </div>
    </div>
  </div>
</div>

这是我的 comment(评论)_form.html.erb

<% if user_signed_in? %>
  <%= form_for [@group, @groups.comments.build] do  |f| %>

    <div class="field">
      <%= f.label :评论 %></br>
      <%= f.kindeditor :commenter %>
    </div>
      <%= hidden_field_tag(:group_id, @group.id) %>

    <div class="actions">
      <%= f.submit "提交"%>
    </div>
    <% end %>
  <% else %>
    <%= link_to "登录后可以添加评论", new_user_session_path %></br>
  <% end %>

这是我的 group.rb

class Group < ActiveRecord::Base
  belongs_to :user
  validates :title, presence: true

  has_many :comments

  has_many :group_relationships
  has_many :members, through: :group_relationships, source: :user

  default_scope  { order(created_at: 'DESC') } # order: 'articles.created_at DESC'
end

这是我的 comment.rb

class Comment < ActiveRecord::Base
  belongs_to :group
  belongs_to :user

  default_scope  { order(created_at: 'DESC') } # order: 'comments.created_at DESC'

end

这是我的 routes

Rails.application.routes.draw do

  devise_for :users

  root 'groups#index'

  resources :groups do
    member do
      post :comments
    end
  end
end


comment 表中 有 commenter user_id group_id 这些字段

这是报错信息:

在这之前一直报错 comment 参数不能为空或 niu 怎么试 都是这个错误 然后我看了 guides(http://guides.ruby-china.org/getting_started.html#creating-the-blog-application )然后修改了_form.html.erb 然后就报现在的错。 我项目的代码地址: https://github.com/babyhai/makeyuan 还请各位朋友帮忙看看

你的 groups 是从哪里来的?是不是 @group.comments

感觉像是 typo,多了个 s

liuminhan 回复

我之前写的 @group.comments 报错说路由错误

这是 写成 @group.comments 的报错

这个路由是有的 但是这个会报错

babyhai 回复

倒过来了,一个是 commets_group, 另一个是 group_commets。

你的 route 文件是怎么写的?

adamshen 回复

我发现了是路由的问题 镶嵌路由需要注意 我重新修改了路由 现在没有这个错误了

resources :groups do
    resources :comments
  end

这是现在的路由

之前的路由加了 member do

现在路由没有问题了 create 出问题了

这是 comment(评论) 的 create

def create
    #@content = commentabel_record.comment.create params[:comment]
    @user = User.find(current_user) #当前用户
    @group = @user.groups.find params[:group_id]

    @comment = @group.comments.create params[:comment]
    @comment.user = @user


    if @comment.save
      redirect_to commentabel_record, notice: '创建评论成功'
    else
      redirect_to commentabel_record, notice: '创建评论失败'
    end
  end
11 楼 已删除

路由已经改了 感谢

babyhai 回复

这是 Rails 默认路由的方法和抽象单元的对应

resources 下的 member 的话是属于 resource 的一个属性的抽象,生成的默认 url_helper 方法的格式是 member_resource_path,action 在 resource 的 controller 里

而 resources 下的 resources 是另一个抽象对象,可以称为 subresources,生成的默认 url_helper 方法是 resource_subresources_path,action 在 subresource 独立的 controller 里

而 group 和 comment,本来就属于两个对象,所以要用 resource 下再套 resource 的声明式,这样和 form_for 默认产生的 url_helper 方法才一致

adamshen 回复

我懂了 谢谢大神

user_id 参数是空的 没传过去

babyhai 回复

可能数据库里确实没有 group_id=1 的数据。

sec 回复

我看了我的表 奇怪的是 评论创建成功了 却没有内容 user_id group_id 都有

这个时候 没有报刚刚 user_id = ? 的错误 现在是:

babyhai 关闭了讨论。 06月07日 14:11
需要 登录 后方可回复, 如果你还没有账号请 注册新账号