如果你做一个rake routes
,你应该看到这样的:
blog_comments GET /blogs/:blog_id/comments(.:format) comments#index
POST /blogs/:blog_id/comments(.:format) comments#create
new_blog_comment GET /blogs/:blog_id/comments/new(.:format) comments#new
edit_blog_comment GET /blogs/:blog_id/comments/:id/edit(.:format) comments#edit
blog_comment GET /blogs/:blog_id/comments/:id(.:format) comments#show
PATCH /blogs/:blog_id/comments/:id(.:format) comments#update
PUT /blogs/:blog_id/comments/:id(.:format) comments#update
DELETE /blogs/:blog_id/comments/:id(.:format) comments#destroy
blogs GET /blogs(.:format) blogs#index
POST /blogs(.:format) blogs#create
new_blog GET /blogs/new(.:format) blogs#new
edit_blog GET /blogs/:id/edit(.:format) blogs#edit
blog GET /blogs/:id(.:format) blogs#show
PATCH /blogs/:id(.:format) blogs#update
PUT /blogs/:id(.:format) blogs#update
DELETE /blogs/:id(.:format) blogs#destroy
第一列有路径帮助程序名称,但没有_url
或_path
后缀。
你:comments
资源嵌套您:blogs
资源内,以辅助名称也嵌套:
blog_comments
不comments
new_blog_comment
没有new_comment
- ...
所以你正在寻找的帮手是blog_comment_url
。此外,由于资源被嵌套,助手会想无论是评论和博客作为参数:
json.url blog_comment_url(comment.blog, comment, format: :json)
使用json.url blog_comment_url当我遇到错误未定义的局部变量或方法'博客”(博客,评论,格式::json) –
我需要修改其他地方像show.json.jbuilder吗? json.partial! “评论/评论”,评论:@comment –
我不确定的背景,所以我不得不猜测。也许你可以说'comment.blog'而不是'blog'。 –