繁体   English   中英

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

[英]Rails 4 routing error with nested comments form using nested resources

我正在按照本教程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”匹配。 如果我到达控制器,然后将相应的“ get”更改为“ post”,则按“提交”后,表单将重新出现,并且没有任何内容添加到数据库中。 我还按照我的教练的建议尝试了浅层嵌套路线,但这没有用。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM