According to the following example, What's the best practice?
controller.rb ...
def index
...
@group = params[:group]
@team = params[:team]
@org = params[:org]
...
end
index.html.haml
= link_to @group, '#'
= link_to @team, '#'
= link_to @org, '#'
controller.rb ...
def index
...
...
end
index.html.haml
= link_to params[:group], '#'
= link_to params[:team], '#'
= link_to params[:org], '#'
Or maybe there is another option, like passing only one instance variable of Hash type...
Thanks!
Strong Parameters, aka Strong Params, are used in many Rails applications to increase the security of data sent through forms. Strong Params allow developers to specify in the controller which parameters are accepted and used.
The Ruby on Rails params are the equivalent of the $_REQUEST array in PHP. Equivalent to $_REQUEST except that $_REQUEST includes get, post and cookie data, while params only get and post data. @renocor POST data is included in the Rails params hash as well. Cookie data is included in the session hash.
With params . Inside your controller action's you can call params to access form & URL query data. What is params , exactly? It's a method that returns an ActionController::Parameters object, in practice it behaves a lot like a hash.
Specifically, params refers to the parameters being passed to the controller via a GET or POST request. In a GET request, params get passed to the controller from the URL in the user's browser.
Generally it's a better idea to split out your parameters as instance variables, especially if you need to do some cleaning up on them. Using params
directly inside a view is a bit messy, and has the effect of needlessly tying the view to the structure of the incoming parameters.
It's the controller's job to intermediate between the incoming parameters and the view itself. It should convert from one format to another so that you can make a change to the parameters without affecting the view, and to the view without changing the requirements for the parameters.
It's unusual to pass through parameters without doing some kind of processing on them. Most of the time, incoming parameters are used to fetch records from a database, or are used in routing in some capacity.
Seeing three parameters being passed in and then used literally on the page is not a common use case. Why are you passing these in instead of passing a reference to one of these things that can be used to determine the others?
For example:
@team = Team.find_by_slug(params[:team_id])
@org = @team.org
@group = @team.group
This is how most Rails applications are constructed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With