Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice - Passing instance variables or using params in Ruby on Rails views?

According to the following example, What's the best practice?

Case 1

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, '#'

Case 2

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!

like image 479
cortex Avatar asked Apr 04 '13 23:04

cortex


People also ask

What is strong parameters in Rails?

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.

How do params work in Ruby on Rails?

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.

What does params do in Rails?

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.

What is params hash in Rails?

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.


1 Answers

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.

like image 84
tadman Avatar answered Nov 16 '22 03:11

tadman