【问题标题】:Rails 5.2. Rendering a js.erb partial from a helper method导轨 5.2。从辅助方法渲染 js.erb 部分
【发布时间】:2018-08-23 15:35:49
【问题描述】:

我有一个名为 Question 的模型,它具有创建操作;
我的目标是在实例无效时使用辅助方法(例如show_alert)立即显示一条消息。

question_controller.rb

def create
    question = Question.new(question_params)
    if question.save then
        redirect_to show_question_path(question.id)
    else
        show_alert(:warning, question.errors)
    end
end

application_controller.rb

    helper_method :show_alert

    def show_alert(type, message)
          @type = type; @msg = message
          respond_to do |format|
            format.js { render :template => 'alert.js.erb'}
          end
    end

alert.js.erb

  var div = $('<div></div>').addClass(`alert alert-${@type}`)
  $('<ul></ul>').append( $('<li></li>').html(@msg)
  div.append(ul)
  $('#alerts').html(div)

但我没有显示闪光灯,而是在白屏上只看到部分代码。

see the screenshot

自从我使用了 respond_to 后,我遇到了另一个错误:ActionController::UnknownFormat

我需要执行alert.js.erb中的sn-p代码,为了渲染flash,我认为诀窍在render函数的某个地方,但是两个小时的谷歌搜索只是浪费时间.

请帮忙!提前谢谢你

【问题讨论】:

  • 我自己也不是什么专家。您能否解释一下创建操作是由 HTML 请求还是由 JS 调用的?你也知道 respond_to 块是如何工作的吗?
  • 我建议保持简单。您应该具有在 html.erb 文件中显示警报的 javascript 函数。如果出现任何错误消息,则应该执行 html.erb 中的 javascript 函数。
  • 你也可以分享你的 alert.js.erb 吗?
  • @HussainNiazi,是的,控制台说Processing by QuestionController#create as HTML,我会尝试使用respond_to

标签: javascript ruby-on-rails


【解决方案1】:

ActionController::UnknownFormat error 出现是因为浏览器正在向 Rails 服务器发送 HTML 请求,但是 respond_to 块只指定了在来自 web 服务器的 javascript 请求的情况下要做什么。

您需要添加一点 Ajax 来实现您想要的。请参阅有关 Ajax 的本教程。 https://www.tutorialspoint.com/ruby-on-rails/rails-and-ajax.htm

Ajax 将在后台向浏览器发送一个 js 请求(即浏览器不会刷新或显示任何加载迹象)。这个 js 请求将被发送到 Rails 服务器,并将包含脚本的 .js.erb 文件返回给浏览器。现在由于这个脚本是作为浏览器对 Ajax 请求的响应返回的,所以浏览器已经知道需要执行的是 javascript。

如果你不想实现 Ajax,你可以在你的创建控制器中做这样的事情:-

def create
    question = Question.new(question_params)
    if question.save then
        redirect_to show_question_path(question.id)
    else
        redirect_to new_question_path(error: question.errors) #new_question_path is the action that displays the question form to the user
    end
end

然后您可以在显示问题表单的操作中初始化一个错误变量。例如

def new
    @error=params[:error]
    #rest of the code... 
end

然后在你的 new.html.erb 的某个地方(或任何 html.erb 文件名)

<script>
    <% if @error %>
        var div = $('<div></div>').addClass(`alert alert-<%= @type %>`)
        $('<ul></ul>').append( $('<li></li>').html(<%= @msg %>)
        div.append(ul)
        $('#alerts').html(div)
    <% end %>
    // you might need to tweak the variable names in controller or the above code
</script>

(上面的代码可能并不完美,只是给你一个想法)

但是这种方法不会像 ajax 那样快速和美观,因为当用户提交他们的问题时,整个页面将再次加载以显示错误警告。

【讨论】:

  • 谢谢!我刚刚在我的表单中添加了remote:true,它就像我想要的那样发送了一个 AJAX 请求!最好的问候!
【解决方案2】:

默认情况下,助手的所有输出都会被转义。要按原样显示 HTMl,您需要使用 html_safe 方法 (https://apidock.com/rails/v4.2.1/String/html_safe)。见Using helpers in a view escapes the html?

【讨论】:

    【解决方案3】:

    如果没有看到您的 alert.js.erb,我无法确定这一点,但您可能需要在 alert.js.erb 中使用 escape_javascript

    alert.js.erb 中的类似(我还没有测试过)

    $('<%= escape_javascript("#{type} - #{msg}") %>').appendTo("#alert")
    

    您可以在Rails Guides - Working With Javascript in Rails上阅读更多信息

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-15
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多