Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

render does not take symbol as template argument

#6083

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.

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

State: invalid
Milestone: none
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. Santiago Pastorino
    Santiago Pastorino
    • 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.

    November 29th, 2010 @ 06:08 PM

  2. James B. Byrne
    James B. Byrne

    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?

    November 29th, 2010 @ 08:30 PM

  3. José Valim
    José Valim

    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.

    November 29th, 2010 @ 10:00 PM

  4. Santiago Pastorino
    Santiago Pastorino

    Because template option expects a '/'

    November 29th, 2010 @ 10:02 PM

  5. James B. Byrne
    James B. Byrne

    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.

    November 30th, 2010 @ 04:06 PM

  6. Neeraj Singh
    Neeraj Singh
    • 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
    

    December 1st, 2010 @ 03:15 AM

  7. Neeraj Singh
    Neeraj Singh

    Never mind. I had a typo.

    as @JV said it expects /index.

    December 1st, 2010 @ 03:55 AM