使用嵌套资源的Rails 4路由错误与嵌套注释表单

时间:2015-02-10 22:30:18

标签: ruby-on-rails forms nested-routes

我正在按照本教程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

我已尝试在表单中设置自定义路由,这会使表单至少显示,但是当您点击提交按钮时,它会返回'No route matches [POST]“/ boards / 1 / comments / new” ”。如果我到达控制器然后将相应的“get”更改为“post”,那么它会导致表单在按下提交后重新出现,并且没有任何内容添加到数据库中。我已经根据我的导师的建议尝试了浅层嵌套我的路线,但这没有用。

1 个答案:

答案 0 :(得分:1)

您的董事会和评论之间的关联必须是:

board.rb

has_many :comments

comment.rb

belongs_to :user

的routes.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