How to get information stored in a form by using ruby-rails


#1

Hi,

I’m a newbie with ruby-rails (also with web
developpement) I want to create a form to get data
from the user, like text field and combobax, button,
etc. I used “text_field_tag” or other “tag” to
genenate those components on the html, but after that
I have no idea how to get the data from the
components, I read the examples and documentations
about how to create the componenets, but I didn’t find
anything which talk about how to extract them… Can
someone please tell me how to do it or where can I
find the documentation!!!

Thanks you very much!!!

sayoyo


Yahoo! FareChase: Search multiple travel sites in one click.


#2

On Nov 9, 2005, at 7:55 AM, Saiho Y. wrote:

anything which talk about how to extract them… Can
someone please tell me how to do it or where can I
find the documentation!!!

Is there any particular reason you’re using ‘tag’ and
‘text_field_tag’? It’s usually much simpler to use the form
helpers. For example, in a view such as edit.rhtml:

<%= text_field ‘user’, ‘name’ %>

and then in your controller you could access the user’s name via:

def edit
@name = params[:user][:name]
end

If you need further help, perhaps you can be more specific with your
question or show examples of what you’ve already tried and why.

Duane J.
(canadaduane)


#3

Hi,

the reason I use “_tag” is because I don’t have a
model (no database) If I understand well the mecanism,
the helpers without “tag”, they send information
directly to the model…so that’s why I didn’t used
them.

After a lot of tryings, I find out that I can get the
info from the variable “@params”, like
"@params[‘post’], but I don’t know if it is the
correct way to get the information.

Thanks you very much

sayoyo

this is an example.

def buildTable
if @params.include?(‘post’) then
@checkTextField = @params[‘post’]
else
@checkTextField = nil
end
end

buildTable.rhtml

Build Table <%= start_form_tag :action => :buildTable %>
<%= text_field_tag(name = "post", nil, "size" =>

“20”, “maxsize”=>“20”) %>

<%= submit_tag(value = "run") %>
<%= submit_tag(value = "cancel") %><br>
<%= end_form_tag %>

<%= @checkTextField %>
<%= debug(@params) %>
</body>

— Duane J. removed_email_address@domain.invalid wrote:

genenate those components on the html, but after
Is there any particular reason you’re using ‘tag’
def edit


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Yahoo! FareChase: Search multiple travel sites in one click.


#4

On Nov 9, 2005, at 1:39 PM, Saiho Y. wrote:

"@params[‘post’], but I don’t know if it is the
correct way to get the information.

Thanks you very much

No problem. Some comments about the code below…

def buildTable
if @params.include?(‘post’) then
@checkTextField = @params[‘post’]
else
@checkTextField = nil
end
end

This method could be simplified, like this:
def buildTable
@checkTextField = params[:post]
end

If there is no ‘post’ key in the params hash, then it will be nil by
default so there’s no need to add the conditional statement.

buildTable.rhtml

Build Table <%= start_form_tag :action => :buildTable %>

You may consider using the convention of underscores instead of
camelCase, e.g.: start_form_tag :action => build_table. It’s a Ruby
convention that Rails sometimes relies on for conversion between
camelCase and underscored variables.

<%= text_field_tag(name = “post”, nil, “size” =>
“20”, “maxsize”=>“20”) %>

<%= submit_tag(value = “run”) %>
<%= submit_tag(value = “cancel”) %>

The above two lines look a little odd to me… you’re assigning the
variable called ‘value’ twice, but then not using it below. Perhaps
you meant to use a symbol-as-key hash like this:

submit_tag(:value => “run”)
submit_tag(:value => “cancel”)

Good luck!
Duane J.
(canadaduane)