How to create a very simple form

Hi all,

I would like to create a very simple form with RoR similar to this one
in ASP.Net:
http://quickstarts.asp.net/QuickStartv20/aspnet/samples/ctrlref/standard/TextBox/TextBox1_vb.aspx

I my RoR project I want to add a Greeter class like this one:
class Greeter
def initialize(name = “World”)
@name = name
end
def say_hi
puts “Hi #{@name}!”
end
def say_bye
puts “Bye #{@name}, come back soon.”
end
end

my form will have two buttons one for “Hi!” and the other for “Bye!”, a
text box to enter a name, and a label to display the whole message. So
if the user enters “Joe” and clicks the button “Hi”, the label will
display “Hi Joe”

How do I create a Viewer that instantiates the class Greeter, and shows
a message after the user click on a button.

In a ASP.Net project in the code behind the button, on the click event I
will have to add something like this:

g = Greeter.new()

= g.say_hi

How can I do it in RoR. I am interesed in the controller and viewer(s).

thanks

Help!!!

Where can I find information about how to add submit buttons to a form?

thanks

Jose P. wrote:

Hi all,

I would like to create a very simple form with RoR similar to this one
in ASP.Net:
http://quickstarts.asp.net/QuickStartv20/aspnet/samples/ctrlref/standard/TextBox/TextBox1_vb.aspx

I my RoR project I want to add a Greeter class like this one:
class Greeter
def initialize(name = “World”)
@name = name
end
def say_hi
puts “Hi #{@name}!”
end
def say_bye
puts “Bye #{@name}, come back soon.”
end
end

my form will have two buttons one for “Hi!” and the other for “Bye!”, a
text box to enter a name, and a label to display the whole message. So
if the user enters “Joe” and clicks the button “Hi”, the label will
display “Hi Joe”

How do I create a Viewer that instantiates the class Greeter, and shows
a message after the user click on a button.

In a ASP.Net project in the code behind the button, on the click event I
will have to add something like this:

g = Greeter.new()

= g.say_hi

How can I do it in RoR. I am interesed in the controller and viewer(s).

thanks

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.

hth,
Bill

----- Original Message -----
From: “Jose P.” [email protected]
To: [email protected]
Sent: Monday, October 02, 2006 7:55 AM
Subject: [Rails] Re: How to create a very simple form

hi again

I would like to convert to Ror this HTML folr:

Enter Your N.:

This is a label 

Bill

To make it simpe lest say that I want to create a form with only on
button, and a text box. Whe the user enters his/her name in the text
box, and clicks the button, a message says “Hello X”

I am just learning and looking for a very simple example without any
database access.

Where can I get any inforation about that?

thanks

Walton wrote:

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.

hth,
Bill

----- Original Message -----
From: “Jose P.” [email protected]
To: [email protected]
Sent: Monday, October 02, 2006 7:55 AM
Subject: [Rails] Re: How to create a very simple form

Max

Adding an initialize method on the class TestController:

class TestController < ApplicationController
def initialize(name = “World”)
@name = name
end
def say_hi
@message=“Hi #{@name}!”
end
def say_bye
@message=“Bye #{@name}, come back soon.”
end
end

Do I need to replace the action “:action=>‘say_hello’” by somethig like
:action=>‘initialize’? how do I get the say_hi method?

Thank you

Muermann wrote:

On 10/3/06, Jose P. [email protected] wrote:

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’ %>

Enter your Name ... <%= @message %>

<%= 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
this:

ruby script/generate controller test

and then you add a method say_hello:

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

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.

Cheers,
Max

On 10/3/06, Jose P. [email protected] wrote:

  @message="Hi #{@name}!"
end

def say_bye
@message=“Bye #{@name}, come back soon.”
end
end

Do I need to replace the action “:action=>‘say_hello’” by somethig like
:action=>‘initialize’? how do I get the say_hi method?

“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
:action=>‘say_hi’.

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.

Cheers,
Max

On 10/3/06, Jose P. [email protected] wrote:

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’ %>

Enter your Name ... <%= @message %>

<%= 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
this:

ruby script/generate controller test

and then you add a method say_hello:

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

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.

Cheers,
Max

Max,

I am gettng an error message:

can’t convert nil into string

here is my controller:
class GreetController < ApplicationController
def say_hi
@message= "Hello " + params[:textName]
end
end

and my viewer

Hello!!! <%= start_form_tag :controller=>'greet', :action=>'say_hi' %>

Enter Your N.: <%= text_field_tag :textName, :size=> 20 %>

<%= @message %>

<%= submit_tag( "Hello") %>

<%= end_form_tag %>

What is wrong?

Muermann wrote:

On 10/3/06, Jose P. [email protected] wrote:

  @message="Hi #{@name}!"
end

def say_bye
@message=“Bye #{@name}, come back soon.”
end
end

Do I need to replace the action “:action=>‘say_hello’” by somethig like
:action=>‘initialize’? how do I get the say_hi method?

“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
:action=>‘say_hi’.

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.

Cheers,
Max

here is the whole error message:

TypeError in GreetController#say_hi

can’t convert nil into String

RAILS_ROOT: script/…/config/…
Application Trace | Framework Trace | Full Trace

app/controllers/greet_controller.rb:3:in +' app/controllers/greet_controller.rb:3:insay_hi’
-e:4:in `load’
-e:4

Jose P. wrote:

Max,

I am gettng an error message:

can’t convert nil into string

here is my controller:
class GreetController < ApplicationController
def say_hi
@message= "Hello " + params[:textName]
end
end

and my viewer

Hello!!! <%= start_form_tag :controller=>'greet', :action=>'say_hi' %>

Enter Your N.: <%= text_field_tag :textName, :size=> 20 %>

<%= @message %>

<%= submit_tag( "Hello") %>

<%= end_form_tag %>

What is wrong?

Muermann wrote:

On 10/3/06, Jose P. [email protected] wrote:

  @message="Hi #{@name}!"
end

def say_bye
@message=“Bye #{@name}, come back soon.”
end
end

Do I need to replace the action “:action=>‘say_hello’” by somethig like
:action=>‘initialize’? how do I get the say_hi method?

“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
:action=>‘say_hi’.

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.

Cheers,
Max

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs