如果你做一個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'。 –