【问题标题】:Reply post comment using Nested Comment from Ancestry gem rails使用 Ancestry gem rails 中的嵌套评论回复帖子评论
【发布时间】:2018-07-11 15:23:35
【问题描述】:

我正在使用 ancestry gem 在我的帖子中创建嵌套评论。

我使用 Rails 5。

这是我的代码:

后模型

has_many :comments

评论模型

  has_ancestry
  belongs_to :post

我在视图/帖子/节目中渲染所有 cmets

<%= render 'comment' %>

内部视图/cmets/_cmets

<%= div_for(comment) do %>
  <%= comment.body %>
  <%= link_to "Reply", new_post_comment_path(:parent_id => comment, :post_id => @post.id) %>
<% end %>

cmets_controller

  def create
    @comment = @post.comments.new(comment_params)
    @comment.user = current_user

    respond_to do |format|
      if @comment.save
        format.html { redirect_to @post, notice: 'Comment was successfully created.' }
        format.json { render json: @comment, status: :created, location: @comment }
      else
        format.html { render action: "new" }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

  def new
    @comment = Comment.new(:parent_id => params[:parent_id])
  end

意见表

<%= simple_form_for new_post_comment_path(@post,@comment)  do |f| %>
    <%= f.hidden_field :parent_id %>
    <%= f.text_area :body, class: "form-control" %>
    <%= f.submit "Leave Comment", class: 'btn btn-main btn-block'  %>
<% end %>

Routes.rb

resources :posts do
    resources :comments
end

rake 路线

           post_comments GET      /:post_id/comments(.:format)            comments#index
                         POST     /:post_id/comments(.:format)            comments#create
        new_post_comment GET      /:post_id/comments/new(.:format)        comments#new
       edit_post_comment GET      /:post_id/comments/:id/edit(.:format)   comments#edit
            post_comment GET      /:post_id/comments/:id(.:format)        comments#show
                         PATCH    /:post_id/comments/:id(.:format)        comments#update
                         PUT      /:post_id/comments/:id(.:format)        comments#update
                         DELETE   /:post_id/comments/:id(.:format)        comments#destroy

在这个网址http://localhost:3000/post_id/comments/new?parent_id=10 ,我点击“回复评论”,它呈现了新的评论表单。 但是如果我点击提交按钮,我得到了这个错误

没有路线匹配 [POST] "/post_id/cmets/new"

请有人帮我解决它。谢谢你

【问题讨论】:

  • 你错过了一条路线,当你运行rake routes时没有POST /:post_id/comments/new(.:format) comments#new。可以发routes.rb
  • 这是我的路线:资源 :posts 做资源 :cmets 结束

标签: ruby-on-rails-5 ancestry


【解决方案1】:

忘记我的评论。我弄混了一些东西。

您的问题在

评论, :post_id => @post.id) %>

new_post_comment_path 解析为 /:post_id/comments/new(.:format) comments#new,因为它是 GET 路径并且没有 POST,所以它不起作用。 你必须发帖到/:post_id/comments(.:format)

既然我现在可以在本地试用,你可以试试

<%= link_to "Reply", post_comment_path(:parent_id => comment, :post_id => @post.id), method: :post %>

或手动添加正确的路径,如

link_to 'foo', :action => :foo, :id => @bar.id, method: :post

link_to 'foo', '/foo/#{@bar.id}', method: :post

【讨论】:

  • comment, :post_id => @post.id) %> ,它指向我没有显示评论路径的评论显示路径先生
  • 做了一些编辑。可能您必须将方法设置为 POST。
猜你喜欢
  • 2014-01-05
  • 2021-09-09
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-17
  • 1970-01-01
  • 2014-06-11
相关资源
最近更新 更多