Je suis tout nouveau sur Rails. J'ai appris comment faire des actions de contrôleurs statiques et des fichiers de vue correspondants. Maintenant, je veux faire en sorte que quand je vais à localhost: 3000/hi/anita, la page dit "Salut anita", mais quand je vais à localhost: 3000/hi/bob, il dit "Salut bob". Bien sûr, je ne veux pas faire des actions anita et bob. Vous avez eu l'idée. Des allusions comment je peux faire ça? Merci!Comment faire pour afficher un fichier .erb Rails la dernière partie d'une URL?
1
A
Répondre
3
Je définirais la route d'accepter la « dernière partie » d'abord, puis le récupérer dans l'action:
# in routes.rb
# for rails 3:
match "/hi/:who" => "static#say_hi"
# for rails 2:
map.connect "/hi/:who", :controller => "static", :action => "say_hi"
# in StaticController
def say_hi
@who = params[:who] || "No body" # as the user can use the url "/hi" without the "last part"
end
# in views/static/say_hi.html.erb
Hi <%= @who %>
Grand, tout ce que j'avais à l'esprit! –