Search code examples
ruby-on-railsjsonrubydevise

respond_to with format.json doesn't find partials


I am overriding devise session controler and while trying to set specific format Rails says there's no partial while it's not true while I am trying to insert it inside respond_to

class SessionsController < Devise::SessionsController
  def new
    self.resource = resource_class.new(sign_in_params)
    clean_up_passwords(resource)
    yield resource if block_given?
    respond_to do |format|
      format.html
      format.json {render(json: { sign_in: render_to_string(partial: 'static_pages/home')})}
    end

  end

The error is:

Missing partial static_pages/_home with {:locale=>[:en], :formats=>[:json], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in: * "D:/project_traveldiary/app/views" * "D:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/devise-4.6.2/app/views"


Solution

  • Looks like it searches for json partial, so try to explicitly specify the format of the partial:

    render_to_string(partial: 'static_pages/home', formats: :html)