简体   繁体   中英

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 _______

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)

只需在模型中声明属性并使用它即可。

attr_accessor :value

如果您在Rails中使用params[:field_name] ,则与PHP中的$_REQUEST['field_name']类似(除了Rails中的params不像$ _REQUEST那样包含cookie),

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM