obtain :title field in controller - ruby-on-rails

I'd like to store page titles in some place for later use. I used nifty-generators to generate layout.
How to access value, which was set by title() method in view? I need to catch it in controller. Is it stored somewhere in response?
or do I need to move title setting to controller action, as I wonder if what I want to do is possible from mvc nature?

If you want to use any variable in more than one request then you should keep it in controller. There are many ways to do this like:
You can pass this variable in URL.
keep it in the session
keep it in the cookie
In rails you can also use flash.now

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.

Get instance variable from previous request in 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.

Display & Update Cart Count on an MVC Layout Menu

I have an MVC application which is supposed to show a Shopping Cart Count on the primary _Layout page. I need to update this value as users add or remove data from their cart. Since these data changes can be done from multiple locations throughout the application, I'm hesitant about putting GetCartCount() calls on each and every controller out there.
I was considering using either a ViewBag or a knockout computed observable to store this value in, but I'm open to any other suggestions.
What is the best way to store & update a single value that will be used on a layout page in an MVC application?
The simplest way would be to put this count variable in the Session and output it directly inside of the _Layout.cshtml using #Session. If you don't want to use session you can put this count variable in the ViewBag with every request, you can set it in the BaseController(if you don't have one just create one and inherit your controllers from it) in OnActionExecuting method and you will avoid duplication.

What is a preferred way of displaying result as a table?

I have an action that populates the result from the DB. Right now, I see a way of doing it and that is to make the Action ServletRequestAware, set the populated list as a request attribute and show it in jsp.
Since a lot of improvements have been introduced into struts2, is there any other way of doing that? The first thing that comes to my mind is to use displayTag and change the return type of Action to a List, but that does not seem to work.
Thanks for any replies in advance.
You question is unclear, you should read some book about Struts2 to get the general idea.
No need to make the Action ServletRequestAware. The mapping from http parameters to actions fields is automatically done via the Param interceptor (already set in the default configuration). And one of the points of Struts2 is decoupling the action from the http protocol, you should not (typically) do anything related to http in your action.
Tipically, in your action execute() method (or whatever) you'll get the data to display from the DB and set it as one property of your action, so that is accesable from some getter.
Then, in your view page (JSP or whatever) you'll display it. You can use displayTag, but first you'll prefer to display it "manually", to understand what's involved. See for example here http://www.roseindia.net/struts/struts2/struts2controltags/iterator-tag.shtml
For manually displaying a table, also see this example http://www.vaannila.com/struts-2/struts-2-example/struts-2-crud-example-1.html , search for the <table class="userTable> tag.

Multiple dynamic method calls or declaring a variable in Grails

In my view, if I have a situation where I need to use a dynamic method (such as Domain.findByName("name")) in multiple places, would it be better to define a variable with and refer to that, rather than have the dynamic method in multiple places? I know this seems like an obvious answer, but I just wanted to make sure Grails doesn't cache it or something, and indeed two DB calls are being made.
By default, grails will only cache "get" requests (i.e. Book.get(4)), if you don't set up any additional caching, you'll hit the database for each request (as you're seeing).
See the 'caching queries' section of the grails documentation for more detail.
If you only want the call to be made once (which makes sense in a view since you'd want it to be consistent), I'd either do the query in the action and pass it in the model, or else you could also use the g:set in your view to set it (though this sounds like it's more appropriate for a controller or service).
It would be better to send the domain object to the view as part of the model, rather than calling Domain.findByName("name") from your view.
So in your controller method you'd want
def myAction = {
def myObject = Domain.findByName("name")
// do other stuff
[myObject: myObject]
}
then in your view you can access it by
${myObject.property}

Resources