评论部分不保存评论

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

我在rails应用程序中发表了评论部分,但是当我发布评论并重新加载页面时,评论不会保存。我没有得到任何错误。

我在routes.rb中有这个:

resources :analyses, only: [:index, :create, :show, :destroy, :update] do
    collection do
      post :destroy_multiple
      get :start_multiple
      post :custom_create
    end

    member do
      resources :comparisons, only: :show, param: :document_id
    end

      resources :comments
  end

然后:

class Comment
  include Mongoid::Document
  field :name, type: String
  field :body, type: String
  embedded_in :analysis
  belongs_to :analysis
end

而在:

class Analysis
  embeds_many :comments

然后:

class CommentsController < ApplicationController
  def create
    @analysis = Analysis.find(params[:analysis_id])
    @comment = @analysis.comments.create(params[:comment].permit(:name, :body))
    redirect_to analyses_path(@analysis)
  end

  def destroy
    @analysis = Analysis.find(params[:analysis_id])
    @comment = @analysis.comments.find(params[:id])
    @comment.destroy
    redirect_to analyses_path(@analysis)
  end
end

_Kmentkhml:

%p= comment.name
%p= comment.body
%p= time_ago_in_words(comments,created_at)
%p
  = link_to 'Delete',[comment.analysis, comment], method: :delete, data: { confirm: 'Are you sure you want to delete this comment?' }

和_form.haml:

= form_for([@analysis,@analysis.comments.build]) do |f|
  %p
    = f.label :name
    = f.text_field :name
  %p
    = f.label :body
    %br/
    = f.text_area :body
  %br/
  %p
    = f.submit

在analyze / show.html.haml中我放了:

%br/
= @analysis.comments.count
Comments
= render @analysis.comments
%h5
Add a Comment:
= render 'comments/form'

在我看不到评论的任何地方我是否犯了错误,或者我是否必须以不同方式做到这一点?

编辑:

Started POST "/analyses/2d1234c6543bca03s444des7/comments" for **** at 2019-02-23 16:28:09 +0100
Processing by CommentsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"***==", "comment"=>{"name"=>"fefwefew", "body"=>"dfwedew"} "commit"=>"Create Comment", "analysis_id"=>"2d1234c6543bca03s444des7"}
MONGODB | localhost:80000 | ***.find | STARTED | {"find"=>"analyses", "filter"=>{"_id"=>BSON::ObjectId('2d1234c6543bca03s444des7')}}
MONGODB | localhost:80000 | ***.find | SUCCEEDED | 0.008967s
Redirected to http://localhost:3000/analyses.2d1234c6543bca03s444des7
Completed 302 Found in 47ms
ruby-on-rails ruby haml
1个回答
0
投票

试试这个:

  def create
    @analysis = Analysis.find(params[:analysis_id])
    @comment = @analysis.comments.create(comment_params)
    redirect_to analyses_path(@analysis)
  end

  private 

  def comment_params 
   params.require(:comment).permit(:name, :body)
  end 
© www.soinside.com 2019 - 2024. All rights reserved.