javascript 有没有办法在不渲染任何其他 js 文件的情况下在 rails 控制器方法中发出警报/弹出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34351999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to alert/popup in a rails controller method without rendering any other js file
提问by Sravan
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
end
结尾
In a controller, I have this method, the ajax call will be made to get to this method and when the condition comes to else, I need an alert/popup saying no users are there to delete, then I can update some other thing.
在控制器中,我有这个方法,将进行 ajax 调用来获取这个方法,当条件出现时,我需要一个警告/弹出窗口,说没有用户可以删除,然后我可以更新一些其他的东西。
Thanks in advance.
提前致谢。
回答by David Runger
Try this in your else
block:
在你的else
区块中试试这个:
render html: "<script>alert('No users!')</script>".html_safe
Note that if you want to include the <script>
tag in a proper HTML layout (with a <head>
tag, etc.), you'll need to explicitly specify a layout:
请注意,如果您想<script>
在适当的 HTML 布局中包含标签(带有<head>
标签等),您需要明确指定一个布局:
render(
html: "<script>alert('No users!')</script>".html_safe,
layout: 'application'
)
Edit:
编辑:
Here's a little more code:
这里还有一点代码:
app/controllers/users_controller.rb:
应用程序/控制器/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:
用户.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:
配置/routes.rb:
Rails.application.routes.draw do
# simply visit localhost:3000 to hit this action
root 'users#delete_users'
end
回答by Richard Peck
You can't invoke a dialogue / popup box directly from the controller; it has to form part of a responseto your browser.
您不能直接从控制器调用对话框/弹出框;它必须构成对浏览器的响应的一部分。
Because Rails is built on the HTTP stateless protocol, each requesthas to be met with a response. Unlike TCP
or Web Sockets
, HTTP only has the capacity to receive ad-hoc responses:
因为 Rails 是建立在HTTP 无状态协议之上的,所以每个请求都必须得到响应。与TCP
或不同Web Sockets
,HTTP 仅具有接收临时响应的能力:
HTTP functions as a request-response protocol in the client-server computing model. A web browser, for example, may be the client and an application running on a computer hosting a web site may be the server. The client submits an HTTP request message to the server. The server, which provides resources such as HTML files and other content, or performs other functions on behalf of the client, returns a response message to the client. The response contains completion status information about the request and may also contain requested content in its message body.
HTTP 在客户端-服务器计算模型中充当请求-响应协议。例如,网络浏览器可以是客户端,而在托管网站的计算机上运行的应用程序可以是服务器。客户端向服务器提交 HTTP 请求消息。提供 HTML 文件和其他内容等资源或代表客户端执行其他功能的服务器向客户端返回响应消息。响应包含有关请求的完成状态信息,还可能在其消息正文中包含请求的内容。
This means that you have deliver any front-end changes to your browserbefore they'll take effect (IE you can't just say "load dialogue" because it won't be sent to the browser):
这意味着您已经在浏览器的任何前端更改生效之前对其进行了更改(即,您不能只说“加载对话”,因为它不会发送到浏览器):
#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 %>
The above code invokes the js.erb
response which can be invoked using respond_to
上面的代码调用js.erb
可以使用的响应respond_to