问题
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