ruby 2.3.3
Rails 5.0.2
centos 7.1
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
<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>
<% 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 %>
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
class Comment < ActiveRecord::Base
belongs_to :group
belongs_to :user
default_scope { order(created_at: 'DESC') } # order: 'comments.created_at DESC'
end
Rails.application.routes.draw do
devise_for :users
root 'groups#index'
resources :groups do
member do
post :comments
end
end
end
这是报错信息:
在这之前一直报错 comment 参数不能为空或 niu 怎么试 都是这个错误 然后我看了 guides(http://guides.ruby-china.org/getting_started.html#creating-the-blog-application )然后修改了_form.html.erb 然后就报现在的错。 我项目的代码地址: https://github.com/babyhai/makeyuan 还请各位朋友帮忙看看