LoginSignup
4
3

More than 5 years have passed since last update.

render :partial で別のフォーマットを使いたい時はfomats

Last updated at Posted at 2017-04-18

formatで指定しても駄目だったので調べた。
ドキュメントに載ってなかったので、該当のメソッド読んだ結果

actionview-5.0.2/lib/action_view/renderer/partial_renderer.rb
    # Sets up instance variables needed for rendering a partial. This method
    # finds the options and details and extracts them. The method also contains
    # logic that handles the type of object passed in as the partial.
    #   
    # If +options[:partial]+ is a string, then the +@path+ instance variable is
    # set to that string. Otherwise, the +options[:partial]+ object must
    # respond to +to_partial_path+ in order to setup the path.
    def setup(context, options, block)
      @view   = context
      @options = options
      @block   = block

      @locals  = options[:locals] || {}
      @details = extract_details(options)

      prepend_formats(options[:formats])

      partial = options[:partial]
      ()

formatsでOKっぽい。

<%= render :partial => "hoge", :formats => :js %>

と書けば良いっぽい。

actionview-5.0.2/lib/action_view/renderer/abstract_renderer.rb
    def prepend_formats(formats)
      formats = Array(formats)
      return if formats.empty? || @lookup_context.html_fallback_for_js

      @lookup_context.formats = formats | @lookup_context.formats
    end

内部的にprepend_formats内で最初にArrayしてるのでオプション自体は配列でもそうでなくても良い様です。

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3