views:

24

answers:

1

I'm brand-spanking new to Rails. I've learned how to make static controller actions and corresponding view files. Now I want to make it so that when I go to localhost:3000/hi/anita, the page says "Hi anita", but when I go to localhost:3000/hi/bob, it says "Hi bob". Of course, I don't want to make anita and bob actions. You get the idea. Any hints how I can do that? Thanks!

+3  A: 

I would define the route to accept the "last part" first and then retrieve it in the 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 %>
PeterWong
Great, just what I had in mind!
Anita