【问题标题】:Is there a way to alert/popup in a rails controller method without rendering any other js file有没有办法在 Rails 控制器方法中发出警报/弹出而不呈现任何其他 js 文件
【发布时间】:2016-03-24 22:46:43
【问题描述】:
def delete_users
  users = User.active.where(:id=>params[:users])
  users.each do |user|
    array = []
    if user.active?
      array << user
    end
  end
  if (array.count > 0)
    user.update_attributes(:status => "inactive")
  else
    "I want an alert/popup here saying no users, when 'delete_users' is called and the condition comes here."
    ........ do other stuff ......
  end  

end  

结束

在控制器中,我有此方法,将进行 ajax 调用以获取此方法,当条件出现时,我需要一个警报/弹出窗口说没有用户可以删除,然后我可以更新一些其他的。

提前致谢。

【问题讨论】:

  • 放入else块return render :text =&gt; "no users are there to delete"

标签: javascript ruby-on-rails ruby


【解决方案1】:

在您的 else 块中试试这个:

render html: "<script>alert('No users!')</script>".html_safe

请注意,如果您想在正确的 HTML 布局中包含 &lt;script&gt; 标记(使用 &lt;head&gt; 标记等),您需要明确指定布局:

render(
  html: "<script>alert('No users!')</script>".html_safe,
  layout: 'application'
)

编辑:

这里还有一点代码:

app/controllers/users_controller.rb:

class UsersController < ApplicationController
  def delete_users
    users = User.active.where(:id=>params[:users])
    array = []
    users.each do |user|
      if user.active?
        array << user
      end
    end
    if (array.count > 0)
      user.update_attributes(:status => "inactive")
    else
      render(
        html: "<script>alert('No users!')</script>".html_safe,
        layout: 'application'
      )
    end
  end
end

user.rb:

class User < ActiveRecord::Base
  # for the sake of example, simply have User.active return no users
  def self.active
    none
  end
end

config/routes.rb:

Rails.application.routes.draw do
  # simply visit localhost:3000 to hit this action
  root 'users#delete_users'
end

【讨论】:

  • 试过但没用。我认为应该将其渲染回模板以使其正常工作。
  • 什么不起作用?这对我有用。我在答案中添加了一些额外的代码。
【解决方案2】:

您不能直接从控制器调用对话框/弹出框;它必须构成对您的浏览器的响应的一部分。


因为 Rails 是基于 HTTP stateless protocol 构建的,所以每个 请求 都必须得到一个响应。不同于TCPWeb Sockets,HTTP只有接收ad-hoc responses的能力:

HTTP 在客户端-服务器计算模型中用作请求-响应协议。例如,网络浏览器可以是客户端,而在托管网站的计算机上运行的应用程序可以是服务器。客户端向服务器提交 HTTP 请求消息。服务器代表客户端提供 HTML 文件和其他内容等资源,或执行其他功能,向客户端返回响应消息。响应包含有关请求的完成状态信息,还可能在其消息正文中包含请求的内容。

这意味着您已将任何前端更改发送到浏览器,然后它们才会生效(IE 您不能只说“加载对话”,因为它不会发送到浏览器):

#app/controllers/your_controller.rb
class YourController < ApplicationController
   respond_to :js, only: :destroy_users #-> this will invoke destroy_users.js.erb

   def destroy_users
      @users = User.active.where(id: params[:users]).count
      if @users.count > 0
         @users.update_all(status: "inactive")
      else
         @message = "No users......"
      end
   end
end

#app/views/your_controller/destroy_users.js.erb
<% if @message %>
  alert(<%=j @message %>);
<% end %>

上面的代码调用js.erb响应,可以使用respond_to调用

【讨论】:

  • 我们不能在控制器动作的中间显示闪现消息吗?
  • 绝对不是。您必须将响应发送回服务器
猜你喜欢
  • 2011-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多