【问题标题】:Rails 4 routing error with nested comments form using nested resources使用嵌套资源的嵌套注释表单的 Rails 4 路由错误
【发布时间】:2015-02-10 22:30:17
【问题描述】:

我正在按照本教程 http://www.sitepoint.com/nested-comments-rails/ 为图像板实现嵌套 cmets。在我让“cmets”属于“boards”之前它工作得很好,然后不得不嵌套我的路线。

这是我的路线:

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/cmets/new” '。如果我到达控制器,然后将相应的“get”更改为“post”,那么它会导致表单在按下提交后重新出现,并且没有任何内容添加到数据库中。我还按照教练的建议尝试了浅层嵌套我的路线,但这没有奏效。

【问题讨论】:

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


    【解决方案1】:

    您在 board 和 cmets 之间的关联必须是:

    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
    

    【讨论】:

      猜你喜欢
      • 2014-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-19
      • 1970-01-01
      • 2014-08-02
      • 2016-04-05
      相关资源
      最近更新 更多