【发布时间】:2013-09-10 08:12:01
【问题描述】:
我有一个 Rails 博客,其中所有帖子都嵌套在类别下,现在我添加了嵌套在帖子下的 cmets,但表单抛出 undefined method `post_comments_path' 错误。
我认为我需要将@posts 设置为类似于@categories.post 但我不确定。
路线
resources :categories do
resources :posts, path: 'article' do
resources :comments, :only => [:create]
end
end
控制器
def create
@post = Post.find(params[:post_id])
@comment = @posts.comments.create!(params[:comment])
redirect_to @post
end
查看
<%= simple_form_for [@post, Comment.new ], :remote => true do |f| %>
<%= f.input :name %>
<%= f.input :email %>
<%= f.input :comment %>
<%= f.button :submit %>
<% end %>
【问题讨论】:
-
尝试运行
rake routes。你会看到最可能的路径是:new_category_post_comment_path
标签: ruby-on-rails ruby-on-rails-3