how is rails params hash made available to controller methods - ruby-on-rails

I understand the rails params hash is available in my controller methods, but I'm trying to understand how that happens. Is it a parameter passed to the method? If so, how can we access it since controller methods don't have any declared arguments?

Its not a parameter, its actually just a 'getter' function, that is declared in https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/http/parameters.rb
So calls to params[:something] is really a method call which returns a hash, from which you access the http parameters.

Related

OGNL needs to call a method

I need to call a method from my action object inside the JSP, something like:
var currentStatus = ${getCurrentStatus()};
I cannot call an attribute, and I tried following this answer (How to call an action method using OGNL) and it didn't work.
There are a variety of ways to call methods (on actions, on other objects, or static methods from classes) from OGNL.
In this case, however, I don't see any issue with using a normal accessor. Note that the JavaBean convention is almost (completely?) about naming. A getter named getCurrentStatus(), accessed simply in OGNL via currentStatus, can contain whatever code you want.
This could include the DB access you mention in your question, etc.

Rails params "method" isn't a method, is it?

In the Rails documentation, the following example is given as a way to display what the server receives from a POST request:
def create
render plain: params[:article].inspect
end
In the subsequent description, the text states
The params method is the object which represents the parameters (or fields) coming in from the form. The params method returns an ActiveSupport::HashWithIndifferentAccess object.
While I understand that all methods are objects, I don't understand how it's correct to refer to the params object as a method. Specifically, the phrase "returns an ActiveSupport::HashWithIndifferentAccess object" suggests to me that there are two calls going on--what in python might look like:
params().__getitem__('article')
but I don't think that's what's actually going on.
The conversation around those lines also refers to params as a method, so I'm starting to think I must be missing something.
I'm new to Ruby, and while I understand that all methods are objects,
No, they aren't. Methods belong to objects (more precisely: methods are defined in modules, and executed in the context of objects), but they are not, by themselves, objects. (It is, however, possible to obtain a reflective proxy which represents the concept of a method by calling the method method, which returns a Method object.)
I don't understand how it's correct to refer to the params object as a method.
Because it is a method. Not an object.
What else would it be? Syntactically, it's obvious that it can only be one of three things: a keyword, a variable, or a method call.
It can't be a keyword, because Rails is just a Ruby library, and Ruby libraries can't change the syntax of the language (e.g. add or remove keywords). It can't be a variable, because in order for it to be parsed as a variable, the parser would need to have seen an assignment to it within the same block.
Ergo, the only thing it can possibly be, is a method call. You don't even need to know anything about Rails to know this. It's just basic Ruby syntax 101.
Specifically, the phrase "returns an ActiveSupport::HashWithIndifferentAccess object" suggests to me that there are two calls going on--what in python might look like:
params().__getitem__('article')
but I don't think that's what's actually going on.
That is exactly what is going on. You call the method params and then you call the method [] on the object that is returned by calling the method params.
This is in no way different from foo.bar: you call foo, then call bar on the return value of foo.
The params method is a method, returns a hash (which holds some details about parameters send to the app). Simplified it looks like this:
def fake_params
{ :controller => 'foo', :action => 'bar' }
end
You can call another method directly on the returned hash like this:
fake_params[:action] #=> 'bar'
params is a method defined in ActionController::Metal which returns the request.parameters object.
https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/metal.rb#L140

HTTParty default_params using a dynamic assigning method

I am using the default_paramsto set a default argument to be appended to all REST calls.
But in my case i have the following:
default_params permanent_argument: self.calculate_permanent_argument
calculate_permanent_argument is a method calculating dynamically the permanent_argument.
Now the issue is that calculate_permanent_argument is called only once when the server restarts, and the value of permanent_argument never changes afterwards.
I want to be able to recalculate the permanent_argumenteach time i do a REST request while using the default_paramshelper.
Any clues?
Thanks!
You're using the class methods for default_params, so it will only be called once.
But there is corresponding instance method for default_params
I guess that you may create instance of your class for query, with it to call the instance method of default_params with self.calculate_permanent_argument
for example, at the controller inside your action method.
I'm just learning to use HTTParty, my guess may not be working.

Passing arguments to nested to_json call

Consider a controller action that returns the following:
Post.includes(:comments).to_json(:include => [:comments])
Is it somehow possible to pass arguments to the to_json comments method call so that I could modify comments json representation in this place rather than doing it in Comment model?
to_json only accepts a list of options, per the documentation... If you absolutely had to, you could monkey-patch to_json to do whatever work you needed to do, then call super to execute the default to_json, but I wouldn't recommend this.
In your situation, based on what I understand you're describing, it's probably best to simply do it either on the Comment model, or as a private method on the controller.

How to pass Parameters to Partials and Components?

I'm pretty new to the Symfony Framework and currently searching for the Best-Practice way to pass Parameters from a search to the included partials.
Currently I'm checking for the Parameter in the executeSearch Method and then setting a variable that is available in the searchSuccess.php File.
In the searchSuccess.php File I'm simply passing the variable to the Partial in the include_partial Method in the array. I keep passing the variable around until I'm in the correct partial.
My Method seems to complicated and wrong, but it seems I can't access the sfRequest variables outside the executeSearch Method?
You can access the request object in any template by calling $sf_request.

Resources