Ошибка маршрутизации Rails 4 с формой вложенных комментариев с использованием вложенных ресурсов

Я следовал этому руководству http://www.sitepoint.com/nested-comments-rails/ для реализации вложенных комментариев для доски изображений. Это работало нормально, пока я не сделал «комментарии» принадлежащими «доскам», а затем мне пришлось вкладывать свои маршруты.

Вот мои маршруты:

Rails.application.routes.draw do
  root "boards#index"
  devise_for :users do
    get '/users/sign_out' => 'devise/sessions#destroy'
  end
  resources :boards do
    resources :comments
      get '/comments/new/(:parent_id)', to: 'comments#new', as: :new_comment
      get '/comments/(:parent_id)', to: 'comments#destroy', as: :delete_comment
      get '/comments/edit/(:parent_id)', to: 'comments#edit', as: :edit_comment
  end
end

Вот моя форма:

<%= form_for [@board, @comment] do |f| %>
  <% if @comment.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@comment.errors.count, "error") %> prohibited this comment from being saved:</h2>

      <ul>
        <% @comment.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <%= f.hidden_field :parent_id %>

  <div class="form-group">
    <% if @comment.parent_id == nil %>
    <%= f.label :title %>
    <%= f.text_field :title, class: 'form-control' %>
    <% else %>
    <% nil %>
    <% end %>
  </div>
  <div class="form-group">
    <%= f.radio_button(:user_id, current_user.id) %>
    <%= f.label(:user_id, "I want to post as myself") %>
    <%= f.radio_button(:user_id, nil) %>
    <%= f.label(:user_id, "I want to post anonymously") %>
  </div>

  <div class="form-group">
    <%= f.label :content %>
    <%= f.text_area :content, class: 'form-control', required: true %>
  </div>

  <div class="form-group">
  <%= f.label :image %>
  <%= f.file_field :image %>
  </div>

  <%= f.submit class: 'btn btn-primary' %>
<% end %>

А вот мой контроллер:

class CommentsController < ApplicationController

  def index
    @comments = Comment.hash_tree
  end

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

  def edit
    @comment = Comment.find(params[:parent_id])
  end

  def create
    if params[:comment][:parent_id].to_i > 0
      parent = Comment.find_by_id(params[:comment].delete(:parent_id))
      @comment = parent.children.build(comment_params)
    else
      @comment = Comment.new(comment_params)
    end

    if @comment.save
      redirect_to root_url
    else
      render 'new'
    end
  end

  def update
    @comment = Comment.find(params[:id])
    if @comment.update(comment_params)
      redirect_to @comment
    else
      render 'edit'
    end
  end

  def make_parent
    @comment.parent_id = nil
    @comment.save
  end


  def destroy
    @comment = Comment.find(params[:parent_id])
    @comment.destroy

    respond_to do |format|
      format.html { redirect_to comments_url }
    end
    authorize! :destroy, @comment
  end

  private

  def comment_params
    params.require(:comment).permit(:title, :content, :user_id, :image)
  end
end

Я попытался установить собственный маршрут в форме, это заставляет форму по крайней мере отображаться, однако, когда вы нажимаете кнопку отправки, она возвращает «Нет маршрута, соответствующего [POST] "/boards/1/comments/new"». Если я доберусь до контроллера, а затем изменю соответствующий «получить» на «сообщение», тогда форма просто появится снова после нажатия кнопки «Отправить», и ничего не будет добавлено в базу данных. Я также экспериментировал с неглубоким вложением своих маршрутов в соответствии с советом моего инструктора, но это не сработало.


person Ian Delairre    schedule 10.02.2015    source источник


Ответы (1)


Ваша связь между досками и комментариями должна быть:

доска.рб

has_many :comments

комментарий.rb

belongs_to :user

маршруты.rb

resources :boards do
resources :comments, only: [:new, :edit, :destroy]
end

это создаст маршрут

new_board_comment   GET    /boards/:board_id/comments/new(.:format)      comments#new
edit_board_comment  GET    /boards/:board_id/comments/:id/edit(.:format) comments#edit
board_comment       DELETE /boards/:board_id/comments/:id(.:format)      comments#destroy
person Archie Reyes    schedule 11.02.2015