Sra*_*van 5 javascript ruby ruby-on-rails
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
Run Code Online (Sandbox Code Playgroud)
结束
在一个控制器中,我有这个方法,ajax调用将进入这个方法,当条件来到其他时,我需要一个警告/弹出窗口说没有用户在那里删除,然后我可以更新一些其他的东西.
提前致谢.
在你的else块中尝试这个:
render html: "<script>alert('No users!')</script>".html_safe
Run Code Online (Sandbox Code Playgroud)
请注意,如果要将<script>标记包含在正确的HTML布局中(带有<head>标记等),则需要明确指定布局:
render(
html: "<script>alert('No users!')</script>".html_safe,
layout: 'application'
)
Run Code Online (Sandbox Code Playgroud)
编辑:
这里有一些代码:
应用程序/控制器/ 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
Run Code Online (Sandbox Code Playgroud)
user.rb:
class User < ActiveRecord::Base
# for the sake of example, simply have User.active return no users
def self.active
none
end
end
Run Code Online (Sandbox Code Playgroud)
配置/ routes.rb文件:
Rails.application.routes.draw do
# simply visit localhost:3000 to hit this action
root 'users#delete_users'
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10819 次 |
| 最近记录: |