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).
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).
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
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
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?
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.
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.
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.
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.
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.