嵌套路线中的嵌套注释

时间:2019-06-14 12:02:20

标签: ruby-on-rails ruby-on-rails-5

我当前正在向应用程序中添加嵌套注释,每次尝试添加注释时,都会出现以下错误Couldn't find Project without an ID,因为路由设置不正确。

现在我想最好的方法是什么?

我可以这样离开我的路线,而只是相应地更改@commentable吗?

我的路线

  resources :projects do
    resources :project_steps, path: "step", only: [:show, :update]
    resources :tasks
    resources :reviews, only: [:create, :destroy]
  end

  resources :tasks do
    resources :comments, module: :tasks
  end

我的评论模型:

  def comments
    Comment.where(commentable: commentable, parent_id: id)
  end

我的Tasks :: CommentsController

class Tasks::CommentsController < CommentsController
  before_action :set_commentable

private
  def set_commentable
    @commentable = Task.friendly.find(params[:task_id])
    # This doesn't work since it can't find a task without a project
  end
end

或者我应该将路线更改为:

resources :projects do
 resources :tasks do
  resources :comments, module: :tasks
 end
end

但是随后出现以下错误:Couldn't find path tasks_comments_path,但随后我需要相应地更改表单:

<%= form_with model: [commentable, Comment.new], local: true, html: { class: local_assigns[:class], data: { target: local_assigns[:target] } } do |form| %>
  <div class="form-group">
    <%= form.text_area :body, placeholder: "Add a comment", class: "form-control" %>
  </div>
  <div class="form-group">
    <%= form.hidden_field :parent_id, value: local_assigns[:parent_id] %>
    <%= form.submit "Create Comment", class: "btn btn-primary btn-block" %>
  </div>
<% end %>

我想念什么吗?我应该如何实现呢?

0 个答案:

没有答案