Is render 'contrib' synonymous with render: action => 'contrib'

Do these two forms of "rendering" have the same effect?

render 'contribute'
render :action => 'contribute'
+3
source share
1 answer

In short: yes, they are the same. However, sometimes passing a string will result in a call to render :fileor render :template.

Here's the API docs for the render function

If we scroll down and click on "Show Source", we will see what it does under the hood.

Notice the block starting at line 872:

872:         elsif options.is_a?(String) || options.is_a?(Symbol)
873:           case options.to_s.index('/')
874:           when 0
875:             extra_options[:file] = options
876:           when nil
877:             extra_options[:action] = options
878:           else
879:             extra_options[:template] = options
880:           end

By looking at this code, we can determine that it is trying to be smart.

  • If a line starts with /, (case when 0), it callsrender :file
  • / ( when nil), render :action
  • / - ( else case), render :template

, : -)

+1

Source: https://habr.com/ru/post/1710026/


All Articles