This project is archived and is in readonly mode.

#6083 ✓invalid
James B. Byrne

render does not take symbol as template argument

Reported by James B. Byrne | November 29th, 2010 @ 05:03 PM

render :template => 'index' Works

render :template => :index does not and gives this error:

  private method `sub!' called for :index:Symbol (NoMethodError)

Should not the template key value be coerced to a string inside the render method? Symbols and strings are interchangeable elsewhere in Rails.

Comments and changes to this ticket

  • Santiago Pastorino

    Santiago Pastorino November 29th, 2010 @ 06:08 PM

    • State changed from “new” to “invalid”
    • Importance changed from “” to “Low”

    template option expect controller/action.
    I think you want to do render :action => :index or just render :index.

  • James B. Byrne

    James B. Byrne November 29th, 2010 @ 08:30 PM

    I think that that appraisal rather misses the point. The issue is one of 'least surprise'. Is there a strong technical reason why symbols should not be allowed as targets to the :template key in the render hash? If not, then why are not symbols permitted to substitute for strings as they generally are elsewhere? Why the exception in this particular case?

  • José Valim

    José Valim November 29th, 2010 @ 10:00 PM

    If render :template => "index" works, that is the bug. We don't expect it to. render :template accepts a path to the view, like "posts/index". render :template => "index" should just work if you have a file at app/views/index.html.erb.

  • Santiago Pastorino

    Santiago Pastorino November 29th, 2010 @ 10:02 PM

    Because template option expects a '/'

  • James B. Byrne

    James B. Byrne November 30th, 2010 @ 04:06 PM

    Then is there a case for the render method to return a specific ArgumentError message in the event it receives a symbol rather than a string as the value for the :template key? I follow that the present error implies that a Sting is expected but given the frequent interchangeability of strings with symbols within Rails perhaps an ArguemntError containing the explicit statement that the :template parameter requires a String argument would be useful.

  • Neeraj Singh

    Neeraj Singh December 1st, 2010 @ 03:15 AM

    • Assigned user set to “José Valim”

    render template => 'index' works because the following method converts options from

    {:template => 'index'}
    

    to

    {:template=>"index", :tempalte=>"index", :prefix=>"users"}
    
    #abstract_controller/rendering.rb
    def _normalize_options(options)
      if options[:partial] == true
        options[:partial] = action_name
      end
    
      if (options.keys & [:partial, :file, :template, :once]).empty?
        options[:prefix] ||= _prefix
      end
    
      options[:template] ||= (options[:action] || action_name).to_s
      options
    end
    
  • Neeraj Singh

    Neeraj Singh December 1st, 2010 @ 03:55 AM

    Never mind. I had a typo.

    as @JV said it expects /index.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages