Rails 中的路由问题

发布于 2024-10-30 08:26:19 字数 4067 浏览 1 评论 0原文

// 请参阅下面的更新

错误:

No route matches {:controller=>"conversations", :action=>"reply", :id=>nil, :board_id=>nil}

参数转储:

{"board_id"=>"2",
 "id"=>"3"}

日志:

Started GET "/boards/2/conversations/3/reply" for 127.0.0.1 at Mon Apr 04 23:40:59 +0200 2011
  Processing by ConversationsController#reply as HTML
  Parameters: {"board_id"=>"2", "id"=>"3"}
  Board Load (0.1ms)  SELECT "boards"."id" FROM "boards" WHERE ("boards"."id" = 2) LIMIT 1
  Board Load (0.6ms)  SELECT "boards".* FROM "boards" WHERE ("boards"."id" = 2) LIMIT 1
Rendered conversations/_reply_form.html.erb (1.3ms)
Rendered conversations/reply.html.erb within layouts/application (9.4ms)
Completed   in 30ms

ActionView::Template::Error (No route matches {:controller=>"conversations", :action=>"reply", :id=>nil, :board_id=>nil}):
    1: <%= form_for(@comment, :url => reply_board_conversation_url(:board_id=>@board_id, :id=>@conversation_id)) do |f| %>
    2:   <% if @comment.errors.any? %>
    3:     <div id="error_explanation">
    4:       <h2><%= pluralize(@comment.errors.count, "error") %> prohibited this reply from being saved:</h2>
  app/views/conversations/_reply_form.html.erb:1:in `_app_views_conversations__reply_form_html_erb__999049254_2171331720_2303070'
  app/views/conversations/reply.html.erb:4:in `_app_views_conversations_reply_html_erb___838091718_2171408600_0'

Rendered /opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.1ms)
Rendered /opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (982.1ms)
Rendered /opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (1001.7ms)

在我的routes.rb中,其:

  get '/boards/:board_id/conversations/:id/reply' => "conversations#reply", :as => :reply_board_conversation
  post '/boards/:board_id/conversations/:id/reply' => "conversations#save_reply", :as => :reply_board_conversation

  resources :boards do 
    resources :conversations
  end

有人知道我做错了什么吗?提前致谢!

// 更新:

找出参数。但是,现在我们有一个新错误..请参阅输出:

Started GET "/boards/2/conversations/3/reply" for 127.0.0.1 at Tue Apr 05 11:29:52 +0200 2011
  Processing by ConversationsController#reply as HTML
  Parameters: {"board_id"=>"2", "conversation_id"=>"3"}
  Board Load (0.2ms)  SELECT "boards"."id" FROM "boards" WHERE ("boards"."id" = 2) LIMIT 1
  Board Load (0.2ms)  SELECT "boards".* FROM "boards" WHERE ("boards"."id" = 2) LIMIT 1
Rendered conversations/_reply_form.html.erb (4.3ms)
Rendered conversations/reply.html.erb within layouts/application (6.3ms)
Completed   in 26ms

ActionView::Template::Error (undefined method `model_name' for NilClass:Class):
    1: <%= form_for(@comment, :url => reply_board_conversation_url(:board_id=>@board.id, :id=>@conversation_id)) do |f| %>
    2:   <% if @comment.errors.any? %>
    3:     <div id="error_explanation">
    4:       <h2><%= pluralize(@comment.errors.count, "error") %> prohibited this reply from being saved:</h2>
  app/views/conversations/_reply_form.html.erb:1:in `_app_views_conversations__reply_form_html_erb__999049254_2174448800_2303070'
  app/views/conversations/reply.html.erb:1:in `_app_views_conversations_reply_html_erb___838091718_2174498080_0'

Rendered /opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.1ms)
Rendered /opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (757.4ms)
Rendered /opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (774.2ms)

// see below for update

Error:

No route matches {:controller=>"conversations", :action=>"reply", :id=>nil, :board_id=>nil}

Parameters dump:

{"board_id"=>"2",
 "id"=>"3"}

Log:

Started GET "/boards/2/conversations/3/reply" for 127.0.0.1 at Mon Apr 04 23:40:59 +0200 2011
  Processing by ConversationsController#reply as HTML
  Parameters: {"board_id"=>"2", "id"=>"3"}
  Board Load (0.1ms)  SELECT "boards"."id" FROM "boards" WHERE ("boards"."id" = 2) LIMIT 1
  Board Load (0.6ms)  SELECT "boards".* FROM "boards" WHERE ("boards"."id" = 2) LIMIT 1
Rendered conversations/_reply_form.html.erb (1.3ms)
Rendered conversations/reply.html.erb within layouts/application (9.4ms)
Completed   in 30ms

ActionView::Template::Error (No route matches {:controller=>"conversations", :action=>"reply", :id=>nil, :board_id=>nil}):
    1: <%= form_for(@comment, :url => reply_board_conversation_url(:board_id=>@board_id, :id=>@conversation_id)) do |f| %>
    2:   <% if @comment.errors.any? %>
    3:     <div id="error_explanation">
    4:       <h2><%= pluralize(@comment.errors.count, "error") %> prohibited this reply from being saved:</h2>
  app/views/conversations/_reply_form.html.erb:1:in `_app_views_conversations__reply_form_html_erb__999049254_2171331720_2303070'
  app/views/conversations/reply.html.erb:4:in `_app_views_conversations_reply_html_erb___838091718_2171408600_0'

Rendered /opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.1ms)
Rendered /opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (982.1ms)
Rendered /opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (1001.7ms)

In my routes.rb, its:

  get '/boards/:board_id/conversations/:id/reply' => "conversations#reply", :as => :reply_board_conversation
  post '/boards/:board_id/conversations/:id/reply' => "conversations#save_reply", :as => :reply_board_conversation

  resources :boards do 
    resources :conversations
  end

Does anyone know what I'm doing wrong? Thanks in advance!

// Update:

Figured out the params. But, now we have a new error.. see output:

Started GET "/boards/2/conversations/3/reply" for 127.0.0.1 at Tue Apr 05 11:29:52 +0200 2011
  Processing by ConversationsController#reply as HTML
  Parameters: {"board_id"=>"2", "conversation_id"=>"3"}
  Board Load (0.2ms)  SELECT "boards"."id" FROM "boards" WHERE ("boards"."id" = 2) LIMIT 1
  Board Load (0.2ms)  SELECT "boards".* FROM "boards" WHERE ("boards"."id" = 2) LIMIT 1
Rendered conversations/_reply_form.html.erb (4.3ms)
Rendered conversations/reply.html.erb within layouts/application (6.3ms)
Completed   in 26ms

ActionView::Template::Error (undefined method `model_name' for NilClass:Class):
    1: <%= form_for(@comment, :url => reply_board_conversation_url(:board_id=>@board.id, :id=>@conversation_id)) do |f| %>
    2:   <% if @comment.errors.any? %>
    3:     <div id="error_explanation">
    4:       <h2><%= pluralize(@comment.errors.count, "error") %> prohibited this reply from being saved:</h2>
  app/views/conversations/_reply_form.html.erb:1:in `_app_views_conversations__reply_form_html_erb__999049254_2174448800_2303070'
  app/views/conversations/reply.html.erb:1:in `_app_views_conversations_reply_html_erb___838091718_2174498080_0'

Rendered /opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.1ms)
Rendered /opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (757.4ms)
Rendered /opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (774.2ms)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情未る 2024-11-06 08:26:19

您提供的日志表明@board_id 和@conversation_id 变量为零。

确保您确实在 ConversationsController 的回复操作中设置了 @board_id@conversation_id 的值。我怀疑您要么正在填充 board_id,要么完全忘记执行 @board_id = params[:board_id] 之类的操作。

更新
为了回答您问题的下一部分,我猜测 @comment 尚未实例化。在控制器操作的某个位置,您应该执行如下操作:

@comment = Comment.new(params[:comment]

这应该从任何现有表单数据创建注释,或者如果没有任何表单数据,则创建新注释。

The log you provided indicates the @board_id and @conversation_id variables are nil.

Make sure you are actually setting the value of @board_id and @conversation_id in ConversationsController's reply action. I suspect you are either populating board_id or forgetting to do something like @board_id = params[:board_id] altogether.

Update
To answer the next part of your question, I am guessing that @comment has not been instantiated. Somewhere in your controller action you should do something like the following:

@comment = Comment.new(params[:comment]

This should create a comment from any existing form data, or a new comment if there isn't any form data.

岛徒 2024-11-06 08:26:19

您的 @board_id@conversation_id 变量都是 nil,正如错误消息中所述:

No route matches {:controller=>"conversations", 
                  :action=>"reply",
                  :id=>nil,
                  :board_id=>nil
                 }

请注意 :id:board_id 参数在这里。

Your @board_id and @conversation_id variables are both nil, as it says in the error message:

No route matches {:controller=>"conversations", 
                  :action=>"reply",
                  :id=>nil,
                  :board_id=>nil
                 }

Note the :id and :board_id parameters here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文