Rails形成帮助和RESTful路由

时间:2010-06-03 02:17:39

标签: ruby-on-rails rest routing html-helper

我有一个像这样的表单部分当前设置来制作新的博客文章

<% form_for([@current_user, @post])  do |f| %>

这在编辑帖子时效果很好,但在创建新帖子时我收到以下错误:

undefined method `user_posts_path' for #<ActionView::Base:0x6158104>

我的路线设置如下:

map.resources :user do |user|
 user.resources :post
end

有没有更好的方法来设置我的部分处理新帖子和编辑当前帖子?

1 个答案:

答案 0 :(得分:1)

map.resources :user do |user|
  user.resources :posts
end

在路线声明中复数您的模型名称。你可以看到它说resourc- es ,所以你必须使用user- s 和post-strong> s 。

控制器应命名为UsersController和PostsController

模型应命名为User and Post。

如果以上示例仍然不适合您,请尝试使用此

map.resources :users do |u|
  u.resources :posts
end
相关问题