我在Rails中遇到了一个问题,我无法解决。我几乎是初学者,但我必须解决以下问题:
我需要在现有的rails web应用程序中构建一种仪表板页面。 此仪表板页面应该能够通过提供2个日期并单击按钮从Oracle服务器启动PLSQL过程。
我尝试使用ruby-plsql
中的rails console
gem,我能够成功调用所需的Oracle存储过程。
我想创建一个带有表单的视图来接受存储过程的日期字段,然后在用户单击提交按钮时执行存储过程。应在提交时启动ruby-plsql
方法。我还需要确保在当前过程正在运行时,可能通过灰显“提交”按钮再次启动已经运行的过程。
如何开发?
答案 0 :(得分:0)
将ruby-plsql
和'haml'宝石添加到Gemfile
:
gem 'ruby-plsql'
gem 'haml'
接下来,您可以使用Rails生成器来控制控制器和支持组件:
rails generate controller procedure execute
在config/routes
中,将get
路由更新为post
路由:
get 'procedure.execute'
post 'procedure/execute'
您可以制作一个简单的视图(此示例使用HAML)。将此代码添加到文件app/views/procedure/execute.html.haml
:
%div
%p#notice
= "A stored procedure is currently running. Please wait..." if @is_running
%div
= field_set_tag "Procedure 1" do
= form_tag procedure_execute_path, id: "form1" do
= hidden_field_tag "proc", "stored_proc_1"
= text_field_tag "date1", nil
= text_field_tag "date2", nil
= submit_tag "Execute", disabled: @is_running
%div
= field_set_tag "Procedure 2" do
= form_tag procedure_execute_path, id: "form2" do
= hidden_field_tag "proc", "stored_proc_2"
= text_field_tag "string1", nil
= text_field_tag "number1", nil
= submit_tag "Execute", disabled: @is_running
%div#results
= @results if @results
%script
$(document).on("ready", register_ajax);
$(document).on("page:change", register_ajax);
确保将jQuery和Unobtrusive Javascript支持添加到/app/assets/javascripts/application.js
:
//= require jquery
//= require jquery_ujs
确认您的config/application.rb
在“应用”类中包含此行:
config.assets.enable = true
请注意,表单现在确定将调用哪个方法,并且它们都将具有不同参数的请求发送到相同的execute
操作。
可以将异步提交表单并处理结果的Ajax代码添加到/app/assets/javascripts/procedure.js
文件中:
var ajax_registered = false;
function register_ajax() {
if (ajax_registered) {
return;
}
$('#form1 input[type="submit"], #form2 input[type="submit"]').click(function () {
// First, disable all of the form buttons and put up a "running" notice
$('form input[type="submit"]').prop("disabled", "disabled");
$("#notice").text("A stored procedure is currently running. Please wait...")
var data = $(this).parent().serialize();
// Submit the Ajax POST
var jqxhr = $.post("/procedure/execute", data, function(data, status, xhr) {
// Success: display the results in the #results div
$("#results").text(data);
}, "text")
.fail(function(data, status, xhr) {
// Notify the user that an error has occurred
alert("An error has occured with the stored procedure");
})
.always(function(data, status, xhr) {
// Always re-enable the submit buttons after completion
$('form input[type="submit"]').prop("disabled", "");
$("#notice").text("");
});
});
ajax_registered = true;
};
接下来,在execute
中实施ProcedureController
操作,该操作应位于app/controllers/procedure_controller.rb
:
class ProcedureController < ApplicationController
@@running = false
def execute
if request.post?
plsql.connection = OCI8.new("hr","hr","xe")
notice = nil
if @@running
notice = "A stored procedure is currently running. Try again later"
else
@@running = true
proc_params = execute_params
proc = proc_params.delete(:proc)
case proc
when "stored_proc_1"
@results = plsql.my_stored_proc(proc_params)
when "stored_proc_2"
@results = plsql.my_other_stored_proc(proc_params)
end
@@running = false
end
if request.xhr?
render text: @results.to_json and return
else
@is_running = @@running
redirect_to procedure_execute_path, notice: notice
end
else
@is_running = @@running
end
end
private
def execute_params
params.permit(:utf8, :authenticity_token, :proc, :date1, :date2, :string1, :number1)
end
end
execute
操作同步运行,但也使用@@ running标志指示它当前正在处理请求,以防在当前正在运行的proc完成之前另一个从仪表板进入。表单中的proc
隐藏字段用于指示要执行的存储过程,并根据选择的控制器进行调度。
如果请求来自Ajax调用,则响应将是一个JSON对象,其中包含存储过程调用的结果;否则,页面将完全呈现,结果将被包含在内。
这应该足够了。它不漂亮,但结构就在那里,您可以改进execute
方法来做其他事情,并根据自己的喜好设计视图样式。