【问题标题】:receive routing error upon submitting comment form提交评论表单时收到路由错误
【发布时间】:2011-05-18 01:19:27
【问题描述】:

从posts#show view cmets只有在第一条评论已经创建的情况下才能添加,否则会抛出这个路由错误:

No route matches {:action=>"show", :controller=>"posts", :view=>"comments"}

终端读取:

Started POST "/comments" for 127.0.0.1 at 2011-05-17 18:18:03 -0700
  Processing by CommentsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"BPeNvVJh+dLBhpiZK16phQHp7UV4MasPyQW6PL9VG8I=", "comment"=>{"post_id"=>"", "parent_id"=>"", "name"=>"", "content"=>"yo"}, "commit"=>"Reply"}
AREL (0.9ms)  INSERT INTO "comments" ("name", "content", "post_id", "created_at", "updated_at", "ancestry") VALUES ('', 'yo', NULL, '2011-05-18 01:18:03.445466', '2011-05-18 01:18:03.445466', NULL)
Completed   in 133ms

ActionController::RoutingError (No route matches {:action=>"show", :controller=>"posts", :view=>"comments"}):
  app/controllers/comments_controller.rb:14:in `block (2 levels) in create'
  app/controllers/comments_controller.rb:9:in `create'

如果帖子的第一条评论已经创建

帖子#show:

<%= render @post %>

<%= nested_comments @comments.arrange(:order => :created_at) %>

<%= render "comments/form" %>

形式:

<%= simple_form_for :comment, :url => { :controller => :comments, :action => "create" } do |f| %>
  <%= f.input :post_id, :required => false, :as => :hidden %>
  <%= f.input :parent_id, :required => false, :as => :hidden %>
  <%= f.input :name, :label => false, :placeholder => "Name (optional)", :required => false %>
  <%= f.input :content, :label => false, :placeholder => "Reply", :as => :text %>
  <%= f.button :submit, "Reply" %>
<% end %>

cmets 控制器:

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

def create
  @comment = Comment.create(params[:comment])
  @comment.save
  respond_to do |format|
    format.html do
      if @comment.errors.present?
        render :new
      else
        redirect_to(post_path(@comment.post, :view => "comments"))
      end
    end
    format.js
  end
end

路线:

root :to => "posts#index"
resources :topics
resources :posts
resources :comments

架构:

create_table "comments", :force => true do |t|
  t.string   "name"
  t.text     "content"
  t.integer  "post_id"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.string   "ancestry"
end

add_index "comments", ["ancestry"], :name => "index_comments_on_ancestry"

create_table "posts", :force => true do |t|
  t.string   "name"
  t.string   "title"
  t.text     "content"
  t.integer  "topic_id"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "topics", :force => true do |t|
  t.string   "name"
  t.datetime "created_at"
  t.datetime "updated_at"
end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 forms routing controller


    【解决方案1】:

    我还没有测试过,但尝试替换

    redirect_to(post_path(@comment.post, :view => "comments"))
    

    在控制器的创建操作中:

    redirect_to(post_path(:id => @comment.post_id, :view => "comments"))
    

    如果上述方法不起作用,我们将尝试其他方法。无论如何,这似乎是您的问题所在。

    【讨论】:

    • ActionController::RoutingError (No route matches {:action=&gt;"show", :controller=&gt;"posts", :id=&gt;nil, :view=&gt;"comments"}): app/controllers/comments_controller.rb:14:in block (2 级) in create' app/controllers/cmets_controller.rb:9:in create'
    • @BasicObject - 你没有post_id,这甚至没有被传递到创建操作中。当您输入 new 操作时,您是否可以在没有任何参数 [:post_id] 的情况下运行此测试?我建议你在尝试保存和重定向之前确保你在@comment 上设置了你的 post_id,否则如果这些 post_id 参数不存在,你将来会让自己容易出错。
    • 那么在我的控制器的创建动作中,我应该在新动作中有类似于@comment的东西吗? @comment = Comment.create(:parent_id =&gt; params[:parent_id], :post_id =&gt; params[:post_id]) ?我还没回家试一试。
    • @BasicObject - 你的创建操作对我来说似乎没问题。相反,您的 new 操作实际上并没有首先传递 post_id 信息来创建。 (我通过查看您的终端,在POST 下,在post_id = "" 下的参数下找到这一点)。确保您将 post_id 传递到您的创建操作中。
    【解决方案2】:

    看起来您的表单没有与实际评论绑定,因为它只是被传递了一个符号。试试这是你的看法

    <%= simple_form_for @comment, :url => { :controller => :comments, :action => "create" } do |f| %>
    ...
    

    实际上,这也应该允许您放弃 :url 参数

    <%= simple_form_for @comment do |f| %>
    

    然后确保在控制器中定义 @comment 并设置 post_id

    @comment = Comment.new
    @comment.post_id = @post.id
    

    【讨论】:

    • 这并没有直接解决 OP 的问题。错误,尤其是终端读数,表明数据已成功提交到 create 操作 - 在保存发生后 重定向时遇到问题。
    • 在我看来 post_idparent_id 没有通过参数传递:"comment"=&gt;{"post_id"=&gt;"", "parent_id"=&gt;"", "name"=&gt;"", "content"=&gt;"yo"} 这应该是它无法重定向到帖子的原因。
    • 你说得对……可能当他/她进入新动作时,OP 没有params[:post_id]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-09
    • 2019-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多