Rails rails 文档中的 engine 无法创建 comments

yutian · 2013年06月04日 · 最后由 yutian 回复于 2013年06月05日 · 2785 次阅读

背景:最近想把项目实现模块化,于是看到了 rails 的 engine 这一章节, 照着文档(http://guides.ruby-china.org/engines.html)走了一半,出现问题就再也走不下去了 问题:走到 3.2 节最后一部分,对 post 添加评论后出现了 route 错误: No route matches {:action=>"show", :controller=>"blorgh/posts"} 路由配置如下:

resources :posts resources :posts do resources :comments end root :to => "posts#index" 项目源码已经推送到 github:[email protected]:rubylab/blorgh.git ,还望各位大哥,大神们不吝指教,@Rei @jasl。在此先谢过诸位!

是不是要在 app 里 mount 一下?

@yanhao 这个 blorgh 还没开发完,在 ~/blorgh/test/dummy 下面使用 rails s 启动运行出的错

@doabit 添加评论的时候报错 http://localhost:3000/blorgh/posts 点击 show 在这个页面添加评论 然后 create comment 提示 Routing Error No route matches {:action=>"show", :controller=>"blorgh/posts"}

@doabit 另外 comment 的内容没有添加进去,数据库中的表 blorghcomments 是空的

#5 楼 @yutian commnet.rb 中你没加

belongs_to :post

你加后试下

@doabit 添加了 还是这个错,会不会是 redirect_to post_path 这个地方的问题。还有就是 @comment = @post.comments.build(params[:comment]) 能保存到 db 中吗?

#7 楼 @yutian redirect_to post_path @post

build 不保存到 db,要 save 一下

:controller=>"blorgh/posts"为啥 controller 是这样,不是应该就是 posts 么。。。你的 controller 难道放在 blorgh 这个文件夹下了?试试给 resources :posts 加上 blorgh 这个 namespace 呢?

@Ryan 这个项目是作为插件到另一个项目用的,需要添加一个命名空间,大概是这样的。

@doabit 感谢 的确是没有保存,添加了一行 @comment.save
最后一行的 redirect_to post_path 修改为 redirect_to @post 就可以了 还是路由没理解 没用好…

需要 登录 后方可回复, 如果你还没有账号请 注册新账号