How to create a very simple form

Hi Jose,

Do you want to add "buttons"? Or "a button"?

The former is not allowed re: the W3C HTML spec. Rails shows you how to do
the latter when you generate scaffolding for an app. Check out, for
example, the new.rhtml view.


All you really need to do is to use a "railsy" form tag and set a
value for the label:

<%= start_form_tag :controller=>'test', :action=>'say_hello' %>
  <p>Enter your Name
  <label><%= @message %></label>
<%= end_form_tag %>

This will render a <form action="/test/say_hello" ...> form tag, which
will then get routed to your test_controller, which you create like

ruby script/generate controller test

and then you add a method say_hello:

class TestController < ApplicationController
  def say_hello
    @message= "Hello " + params[:textName]

This assigns a string to the @message instance variable of the
controlle,r which then gets rendered in the view inside the label.

I'd also recommend reading a few RoR tutorials to get your head around
the MVC pattern as implemented in Rails. Without understanding what
MVC means and how it works, you won't get very far with Rails.


"initialize" is the name for the constructor in ruby. You should not
really need to implement a constructor for what you are doing, and you
should definitely not call "initialize" as an action from your view.

When a request comes in, rails will execute the method corresponding
to the action name, so to just call say_hi, you can use

As you want to have different behaviour depending on which button was
pressed, you can use the fact that the browser will send the value of
the submit button that was used to submit the form.

Print out the contents of the params array as the first statement in
your action (p params) and see if you can find the button in there.
From theree, it is easy enough to write an if statement that - based
on the existence of a specific button value in the params - sets the
message to the correct one.

Give it a try and let me know if you get stuck.