【问题标题】:Help convert a nested route to Rails 3帮助将嵌套路由转换为 Rails 3
【发布时间】:2010-08-22 20:59:02
【问题描述】:

我有一个评论表单(在 cmets/_form.html.erb 中),我在其他控制器(帖子和标签)中使用它。

<% form_for([@post, Comment.new], :html => { :multipart => true }) do |f| %>

    <%= f.text_field :commenter %>

    <%= f.text_field :email %>

    <%= f.text_area :body %>

    <%= f.submit 'submit' %>

<% end %>

在我的评论模型中,我有:

belongs_to  :post

在我的应用程序的 rails 2 版本中,我的 routes.rb 包含 map.resources :posts, :has_many =&gt; :comments,它运行良好,但 Rails 3 中的相同配置会引发未定义的方法错误:

undefined method `post_comments_path' for #<#<Class:0xf94920>:0xf8d540>

我认为 Rails 2.x 路线只是在 3.1 发布之前贬值。如何将其转换为 Rails 3?感谢您阅读我的问题。

【问题讨论】:

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


    【解决方案1】:

    在 Rails 3 中,您可以这样定义嵌套路由:

    resources :posts do
      resources :comments
    end
    

    我认为您可能还需要稍微不同地定义 form_for:

    <%= form_for [:post, @comment] do |f| %>
    

    希望对您有所帮助!查看http://rizwanreza.com/2009/12/20/revamped-routes-in-rails-3 了解有关 Rails 3 中路由的更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-15
      • 2011-09-07
      • 2015-02-03
      • 1970-01-01
      • 2011-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多