如何在 rails 命名路由上使用 ruby [英] How to use ruby on rails named routes
问题描述
我定义了这样的嵌套资源
I defined nested resources like this
resources :item, :only => [:create, :destroy, :update] do
resources :item_image, :only => [ :new, :create, :show , :destroy, :index]
end
我的路线是这样的(rake 路线的输出)
And my routes look like this (output of rake routes)
item_item_image_index GET /item/:item_id/item_image(.:format) item_image#index
POST /item/:item_id/item_image(.:format) item_image#create
new_item_item_image GET /item/:item_id/item_image/new(.:format) item_image#new
item_item_image GET /item/:item_id/item_image/:id(.:format) item_image#show
DELETE /item/:item_id/item_image/:id(.:format) item_image#destroy
我认为输出的第一列是命名路由".
I thought the first column of the output is "the named routes".
我想在我的一个视图中显示/item/:item_id/item_image(.:format) 的路径.
I want to show a path to /item/:item_id/item_image(.:format) in one of my view.
item_item_image_index GET /item/:item_id/item_image(.:format) item_image#index
我试过了:
<%= link_to "users", item_item_image_index %>
还有这个
<%= link_to "users", item_images_path %>
都不行
我收到未定义的局部变量或方法`item_images_path/item_item_image_index'"错误
I got "undefined local variable or method `item_images_path/item_item_image_index'" error
推荐答案
你应该尝试:
<%= link_to "users", item_item_image_index_url(@item) %>
或
<%= link_to "users", item_item_images_url(@item) %>
或
<%= link_to "users", item_item_image_index_path(@item) %>
或
<%= link_to "users", item_item_images_path(@item) %>
不要忘记 url 需要一个 :item_id,因此您需要传递一个项目作为参数.
don't forget the url needs an :item_id, hence you need to pass an item as an argument.
实际上,您应该避免将该模型命名为ItemImage".一个项目有图像,这就是你需要知道的.你会得到更好的帮助名称,比如item_images_url"
actually, you should avoid naming that model "ItemImage". An Item has Images, that's what you need to know. you'll get better helper names like "item_images_url"
这篇关于如何在 rails 命名路由上使用 ruby的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!