我制作了一個腳手架并制作了班級“專案”。這使得 routes.rb 成為資源。我做另一條路線
Get 'projects/status/:id', to 'projects#status'
如何在 HTML 代碼上呼叫此鏈接?我試過
<%= link_to "show status", '/status'project.id %>
但它不起作用。請幫忙
uj5u.com熱心網友回復:
要鏈接到您定義的路徑,請使用帶有插值的純字串作為:id引數
# GET /projects/status/:id
# config/routes.rb
get 'projects/status/:id', to: 'projects#status'
# view
<%= link_to 'status', "/projects/status/#{@project.id}" %>
您可以使用:as選項添加路徑輔助方法。最好附加status路徑以使其與 Rails 約定保持一致并且不與:id引數沖突。這類似于其他project路線,例如edit和new。現在您有了status專案的路線。
# project_status GET /projects/:id/status
# config/routes.rb
# NOTE: `as: :project_status` will create `project_status_path` and `project_status_url` helpers
get 'projects/:id/status', to: 'projects#status', as: :project_status
# view
# NOTE: path helper will automatically extract `:id` param from @project
<%= link_to 'status', project_status_path(@project) %>
路由可以嵌套在專案資源下。有關所有選項,請參閱: https ://api.rubyonrails.org/v7.0.2.3/classes/ActionDispatch/Routing/Mapper/Base.html#method-i-match
# status_project GET /projects/:id/status
# config/routes.rb
resources :projects do
get :status, on: :member
end
# view
<%= link_to 'status', status_project_path(@project) %>
# TODO: maybe fix backwards helper name
# get :status, on: :member, as: :state_of
# => state_of_project_path(@project)
查看您的路線
bin/rails routes
# helper name # url # controller#action
project_status GET /projects/:id/status(.:format) projects#status
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/460252.html
