How do I alias a rails route with an id?

孤人 提交于 2020-06-13 06:48:46

问题


I have these routes:

get '/welcome', to: redirect('/welcome/1')
get 'welcome/:id' => 'users#welcome'

The redirect route does a 301 to /welcome/1.

Instead of doing a redirect I'd like it to just execute users#welcome with an id of 1.

How do I do this?


回答1:


You need:

get 'welcome', to: 'users#welcome', defaults: { id: 1 }



回答2:


You can give pathname to the route and pass the id as parameter like this.

get 'welcome/:id' => 'users#welcome', as: :welcome

and pass the id as parameter like this

<%= link_to 'Button', welcome_path(id) %>


来源:https://stackoverflow.com/questions/40720807/how-do-i-alias-a-rails-route-with-an-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!