Get instance variable from previous request in rails - ruby-on-rails

I have an instance controller containing data which i need to filter in a separate function within the same controller. From what I've read this seems to be impossible, since the lifespan of an instance variable is only in the request. So in what way can i get a hold of the data in the instance controller which was served in the previous request? I know that you can use hidden fields and store it in session but there has to be a better way?

Use flash?
http://guides.rubyonrails.org/action_controller_overview.html#the-flash
flash[:my_object] = json_object
I don't think it is good for complete objects but maybe store the entire object in your DB and falsh the id. Or just flash some key parameters.

Related

Rails: Is it possible to use a session variable in a rails controller?

I want to know if it is possible to define a value for a session variable in a view page (.html.erb) and use it in a controller?
For example:
in order controller, new.html.erb:
session[:amount] = #order.amount
in payment controller file:
#amount = session[:amount]
I have a variable in my controller which its value should be changed based on variable I get in one of the views. As the value is stored in a session, I need to use the session value in my controller. Thank you in advance for descriptions and replies.
As I tried, I found that it's possible to pass a parameter from a view to a controller using a session variable. The problem I had was due to the type of the variable's value. I used a session variable and changed its type by using the "flood" function, and the problem is solved.
I asked the question in its general form to know more about the session variables but unfortunately I've not received proper answers.
You say
which its value should be changed based on variable I get in one of
the views
How does the view get the value ?
If you get it by user input:
Using ruby in the view won't help, since the user is interacting with a HTML page. You should use a form POST, possibly asynchronous to make a server call which will set the new value to the rails session.
If you get it by calculating something in ruby:
Then you could and should probably do it from the controller and not from the view. And yes, you can store values in the rails session.
Also, if you are not sure where your problem is (either session usage or Stripe API usage), I suggest you isolate both problems to figure out a solution.
For instance try setting any hardcoded value in the session in one controller method then reading it in another one. When that works, use it in combination with Stripe.
The problem you are facing is to have a persistence data across multiple requests.
You cannot set a session in the rails view. As the whole principle of session is to have persistent data on the server side. To tackle your problem, you can make use of cookies. Cookies are used for storing persistent data across the request on client side and are sent to the server with each server request. Setting a cookie in your view and using it in a controller will serve your purpose.

Passing instance variable between methods in same controller not working in Rails

I have a multi stage form I'm trying to process. One of the later stages needs to use data set by a previous stage. I had thought that by setting the data I need in an instance variable, e.g.
#gateway = importer.get_gateway
I could then access it in a different controller method of the same controller class, e.g.
Rails.logger.info "populate_devices : gateway is #{#gateway}"
However in the logs #gateway is nil in this next step of the form submission. Should it be working, or is this not the correct way to pass data from one controller method to another in the same controller class? #gateway is definitely being set to not nil in the previous step as I display some of it's fields on the UI and have logged it.
I think you should store it in session, because Rails is a stateless technology. There are limitations in size, but only if you choose to use a cookie session store (up to 4k).
If you decide to use the ActiveRecord session store or memcached store, there are no limits. They are easy to implement and fast.

Handling MVC Session for every page

In fairly new to MVC and I would like to use a session. I have a base controller and all my other controller inherit from my base. I need the session checked every time a page is hit.
What is the best way to go about this?
Updated
My session will need to store an id to be able to build the pages correctly. If the session doesn't have the ID I need to look up the information in DB. I don't want to use cache because IDs could be different for different users.
I recommend going the cache route.
Create a class called 'CacheHelper' and within it, a method called 'GetId()'
In the GetId() method, setup a Dictionary object to store your values and use the username as the key.
Each time you call GetId, check to see if the Key exists in your dictionary
myDictionary.ContainsKey(username);
If not, look it up in the database, add it to the dictionary, then resave it to cache.

How should I store variables between method calls in rails?

I'm fairly new to RoR I have a controller and within that controller I need to store an instance variable I create in the new method and reference it again in the create method.
Now because HTTP is stateless the only reasonable way I have found to do this is store the id of the model that the instance variable contains in session[] and then recall this value from session[] again in the create method. I'm just concerned about security using this way of storing the variable's id, I need to make sure that a user can't change the value of what I've stored in session[]
Is there a better way for me to do this? Is it safe? Or should I try something else?
If you can't recalculate that model id on every request, using a session is ok.
Here's more info on session security in Rails.
if you can load the model in the new method, load it from create using the same way you load it in new

ASP.NET binding object to Request in asp.net mvc

I've created a object that I'd like to have accessible from wherever the request-object is accessible, and to "die" with the request, more or less like how you always in a mvc-application has access to the RouteData-collection. Especially it's important that I have access to this object in the execution of action-filters. And also there need to be created a new object of my class whenever a new request is made to the page (the object needs to be request-safe, ie. only one request modifies that one object).
Any thoughts about how to achieve this?
HttpContext is a good place for this. The Items dictionary could be used to store objects relative to the request.

Resources