Kevin H
Kevin H

Reputation: 599

Ruby On Rails: access a variable from a form, without saving it to database

I have 3 pages:

[Page 1] Index

[Page 2] User enter value in a form.

[Page 3] Do calculation on the value entered and output it on this page.

I as wondering how do i do this? I do not want to save the data in any database.

Thank you =)

*Added info 20100628 based on feedback. Thank you! *

for PHP, I could user $_GET and %_POST function. I am not sure how to do it on ROR. [Page 1: index.html]

Amount in bank: Interest rate (%):

[Page 2: interest_cal.php] In 10 yrs, the total amount in your bank would be _______

Upvotes: 0

Views: 3831

Answers (6)

sameera207
sameera207

Reputation: 16629

Try something like this

Page1 -> index

Page2 ->

say you have a model called user which is not inherited from ActiveRecord, so it will be looks like this

class User
  attr_accessor :my_value   
end

and you will have a form like this

<% form_for(@user) do |f| %>
  <p>
    <%= f.text_field :my_value %>
  </p>
  <p>
    <%= f.submit 'send value' %>
  </p>
<% end %>

** sorry i had to remove some tags

and in your form post action (in controller), you can get the value as

params[:user][:my_value]

hope this helps

cheers

sameera

Upvotes: 0

PeterWong
PeterWong

Reputation: 16011

If you don't have a model, but just simply a variable, I would prefer using form_tag instead of form_for

<%= form_tag :action => "your_page_3_action" $>
    <%= text_field_tag :some_var %>

Something like that. And then you could get back the :some_var through:

def your_page_3_action
  params[:some_var] # that's your input valut

Upvotes: 0

Falcon
Falcon

Reputation: 1463

I do not want to save the data in any database.

its simple don't create any model. or if you must create model. create model without inheritance from ActiveRecord::Base

class UserModel 

but not

class UserModel <  ActiveRecord::Base

for PHP, I could user $_GET and %_POST function.

use params variable in controller

Upvotes: 0

John Douthat
John Douthat

Reputation: 41209

if you use params[:field_name] in Rails, this is similar to $_REQUEST['field_name'] in PHP (except params in Rails doesn't include cookies, as $_REQUEST does)

Upvotes: 0

valodzka
valodzka

Reputation: 5805

Just declare attribute in your model and use it.

attr_accessor :value

Upvotes: 0

jigfox
jigfox

Reputation: 18177

welcome to stackoverflow. This is a programming QA Website, so it would be nice to see some Code, to see more clearly what You want. But I think what you want is a tableless model. There are two good screencasts by railscasts:

  1. Tableless Model (Rails 2)
  2. Active Model (Rails 3)

Upvotes: 2

Related Questions