简体   繁体   English

将(Sinatra)Ruby值传递给Javascript

[英]Passing a (Sinatra) Ruby value to Javascript

So, i'm making a subscribe form. 所以,我正在订阅表格。

Jquery jQuery的

    $("<div id='dialog' title='Subscribe!'> <form id='subscribe_form' method='POST' action='/user/subscribe'>" +
        "<input type='text' name='subscribe_email' id='email' placeholder='Email Address'> <br/>" +
        "<button id='submit_subscribe_form'>Submit</button></p><p id='ruby_bool'></p></form>" +
        "</div>").appendTo($("#subscribe"));

When this form is submitted, it sends an ajax call to a Ruby Sinatra listener (sorry if I'm not using the right terminology, haven't really been taught Sinatra, just shown how to use it) 提交此表单时,它会向Ruby Sinatra监听器发送一个ajax调用(对不起,如果我没有使用正确的术语,还没有真正教过Sinatra,只是展示了如何使用它)

 $('form').submit(function(){
        $.ajax({
            type: "POST",
            url: "/user/subscribe",
            data: $('form').serialize(),
            success: function()
            {

Ruby Code Ruby代码

 post "/user/subscribe" do

user_Information = EmailList.new

if params[:subscribe_email] =~ /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/
  user_Information.email = params[:subscribe_email]
  puts user_Information.save
  @email_validation_result = "True"
else
  @email_validation_result = "False"
end

puts @email_validation_result

(Yes i know i shouldn't use regex, but the engines i could find were for PHP) I want to use the @email validation result so i can know what to put in my success: call in my ajax. (是的,我知道我不应该使用正则表达式,但我能找到的引擎是用于PHP)我想使用@email验证结果,所以我可以知道在我的成功中放什么:在我的ajax中调用。 Problem is, JavaScript doesn't allow Ruby Injection (according to my god knows how many hours of research) and i cant update a div on the web page that contains that variable async. 问题是,JavaScript不允许Ruby注入(根据我的上帝知道多少小时的研究)并且我无法在包含该变量异步的网页上更新div。 I want to do this all async, so there is no refreshing of the entire page whatsoever. 我希望所有这些都是异步的,所以整个页面都没有任何刷新。 (If it's not possible otherwise i will concede, but i highly doubt that). (如果不可能,否则我会承认,但我非常怀疑)。 I tried to put the div on another page and use the JQuery .load() function, but .erb files aren't recognizable. 我试图将div放在另一个页面上并使用JQuery .load()函数,但.erb文件无法识别。 Out of ideas and nearly out of sanity. 出于想法,几乎出于理智。 Thanks! 谢谢!

JavaScript: JavaScript的:

$.post( '/user/subscribe', $('form').serialize(), function(data){
  // Do whatever you want with the response from the server here
  // data is a JavaScript object.
}, 'json');

Ruby/Sinatra: 红宝石/西纳特拉:

require 'json' # just for a convenient way to serialize

post '/user/subscribe' do
  # process the params however you want

  content_type 'application/json'
  { :ok => @is_ok }.to_json
end

Without the JSON library you could end your method with just some valid JSON markup, like: 如果没有JSON库,您可以使用一些有效的JSON标记来结束您的方法,例如:

%Q[ { "ok":#{@is_ok} } ]

JavaScript/AJAX will post to the server, the matching Sinatra route will process the request, and the string result of that method ( not done via puts ) will be sent as the response to the method. JavaScript / AJAX将发布到服务器,匹配的Sinatra路由将处理请求,并且该方法的字符串结果( 通过puts完成)将作为对该方法的响应发送。 The jQuery AJAX handler will receive it, parse it as JSON and invoke your callback function, passing the JavaScript object it created as the parameter. jQuery AJAX处理程序将接收它,将其解析为JSON并调用您的回调函数,并将其创建的JavaScript对象作为参数传递。 And then you can modify your HTML DOM as desired, client side. 然后,您可以根据需要修改HTML DOM,即客户端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM