views:

77

answers:

1

Do these two forms of 'render' have the same effect?

render 'contribute'
render :action => 'contribute'
+1  A: 

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

Here's the API docs for the render function

If we scroll down and click 'Show Source' we can see what it's doing under the hood.

Note 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 the string starts with a /, (the when 0 case) then it will call render :file
  • If the string doesn't contain a / at all, (the when nil case) then it will call render :action
  • If the string contains a / somewhere in the middle or end of the string (then else case), then it will call render :template

Hope this answers your question satisfactorily :-)

Orion Edwards
Now that's an answer. Thanks.
Owen Sochurne