【问题标题】:respond_to with format.json doesn't find partials使用 format.json 的 respond_to 找不到部分
【发布时间】:2019-07-29 13:59:47
【问题描述】:

我正在覆盖设计会话控制器,并且在尝试设置特定格式时 Rails 说没有部分,但它不是真的 当我试图将它插入到 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

错误是:

缺少带有 {:locale=>[:en], :formats=>[:json], :variants=>[], :handlers=>[:raw, :erb, :html, 的部分 static_pages/_home构建器,:ruby,:coffee,:jbuilder]}。在以下位置搜索:*“D:/project_traveldiary/app/views”*“D:/RailsInstaller/Ruby2.3.​​3/lib/ruby/gems/2.3.0/gems/devise-4.6.2/app/views”

【问题讨论】:

  • 请显示您的部分文件的路径(相对于项目根目录)
  • app/views/static_pages/_home.html.erb
  • 如果我不使用respond_to,它会正确找到它
  • 您有什么理由想要将 HTML 页面呈现为 JSON 响应的一部分?这似乎是一种奇怪的反应。
  • 我想切换到一个页面注册并使用一个按钮登录,所以我需要以某种方式将此部分呈现为 JSON,以便稍后使用 AJAX @engineersmnky 捕获它,您对此有何看法?还有其他方法吗?

标签: ruby-on-rails json ruby devise


【解决方案1】:

看起来它在搜索json partial,所以尝试明确指定 partial 的格式:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    • 2015-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多