简体   繁体   English

失去焦点时在textarea中保存和显示文本Rails 2.3.12

[英]Saving and displaying text in textarea when focus is lost Rails 2.3.12

I have no previous experience with Javascript/JQuery/AJAX and I was given a task on our current project at my internship that relies heavily on all three! 我以前没有使用Javascript / JQuery / AJAX的经验,在实习期间我得到了当前项目的一项任务,该任务很大程度上取决于这三个方面! Our client has a site where users create profiles, but apparently a lot of users leave the page or refresh before they save the form and everything gets deleted. 我们的客户有一个站点,用户可以在其中创建配置文件,但显然许多用户在保存表单并删除所有内容之前离开页面或刷新页面。

I am supposed to make it so every time a user changes focus from the text area they are using, the text they have entered so far saves and is reloaded when they refresh the page, or whatever they are doing to lose everything. 我应该做到这一点,以便每次用户从使用的文本区域更改焦点时,到目前为止输入的文本都会保存并在刷新页面时重新加载,或者他们正在做的一切都会丢失。 From what I have been told I need to make a JQuery function that does an AJAX post and then get, but I have no idea how to implement Javascript/JQuery/AJAX into HAML and Rails projects, let alone how to use any of the JS that I'm supposed to. 据我所知,我需要制作一个执行AJAX发布然后获取的JQuery函数,但是我不知道如何在HAML和Rails项目中实现Javascript / JQuery / AJAX,更不用说如何使用任何JS了。我应该的。

I realize not all the information needed to completely solve the problem is given here, I'll happily supply that if someone asks, but I thought it would be best to just implement an alert function when the textarea loses focus and start small. 我意识到这里并没有提供完全解决问题所需的所有信息,我会很乐意提供有人询问的信息,但是我认为最好是在文本区域失去焦点并从小处开始时实施警报功能。

Thanks in advance! 提前致谢!


*Revision #1 *版本1


Okay, I filled in what Tejs gave me and have: 好的,我填写了Tejs给我的东西,并且有:

:javascript
  function post_text() {
    $(document).ready(function() {
      $.ajax({ 
        var currentTextBoxValue = $(textarea).val();
        var currentStatus = $(input).val();

        type: 'POST',
        url: '/profiles/:profile_id/profile_acts',
        data: ''
        success: function() {
          alert("Hello");
        }
      });
    });
  }

I currently have this at the bottom on the page and have {:onblur => "post_text()"} attached to the text_area_tag I want to get the data from. 我目前在页面底部,将{:onblur => "post_text()"}附加到要从中获取数据的text_area_tag I got the URL from rake routes and I know that the data should be sent in JSON. 我从rake routes获得了URL,我知道数据应该以JSON发送。 I know that I need to send some JSON, but I need to include the profile_act_id's and don't want to write a script for each act that we have and I'm not sure how I could incorporate Ruby class variables within the code. 我知道我需要发送一些JSON,但是我需要包含profile_act_id's并且不想为我们的每个行为编写脚本,而且我不确定如何将Ruby类变量合并到代码中。

As for what needs to be sent with JSON, I know that it needs to be something like this: 至于需要使用JSON发送的内容,我知道它必须是这样的:

profile_acts:{"profile_act_40":{
  "act_id":"40", 
  "additional_info":currentTextBoxValue, 
  "status":currentStatus
  }
}

Ideally it would be something like this: 理想情况是这样的:

profile_acts:{"profile_act_" + #{act.id}: {
  "act_id":#{act.id},
  "additional_info":currentTextBoxValue,
  "status":currentStatus
  }
}

I'm pretty sure I can't chop profile_act_40 into "profile_act_" + #{act.id} , but there's got to be an easier way to do this. 我敢肯定我不能将profile_act_40 "profile_act_" + #{act.id} ,但是必须有一种更简单的方法。

Once I get this figured out I still am not sure what I would need to incorporate into the profile_acts_controller.rb. 一旦弄清楚了这一点,我仍然不确定需要将什么内容合并到profile_acts_controller.rb中。

Once again, thank you in advance. 再次感谢您。


*Revision #2 *修订版2


After working on it a bit more and getting help from my coworker I have this: 经过更多的工作并从我的同事那里获得帮助后,我得到了以下信息:

:javascript
  $(document).ready(function() {
    jQuery.ajaxSetup({  
      'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}  
    });

    ajaxPost = function (_url, _dataString) {
      $.ajax({
        type: 'POST',
        url: _url,
        data: _dataString,
        success: function() {
          alert("Success!");
        }
        failure: function () {
          alert("Failure!")
        }
      });
    };

    getId = function(object) {
      var _id = $(this).closest(".act").attr("act_id");
      alert("ID got!");
      return _id;
    }

    getInfo = function(object) {
      var _info = $(this).value();
      alert("Info got!");
      return _info;
    }

    getInfoString = function(_id, _info) {
      var _infoString = '"profile_acts":{"profile_act":{"act_id":_id, "additional_info":_info}}';
      alert("Info string got!");
      return _infoString;
    }

    getStatusString = function(_id, _status) {
      var _statusString = '"profile_acts":{"profile_act":{"act_id":_id, "status":_status}}';
      alert("Status string got!")
      return _statusString;
    }

    getUrl = function(_id) {
      var _url = "/profiles/" + _id + "/profile_acts";
      alert("URL got!");
      return _url;
    }

    showAlert = function(msg) {
      alert(msg);
    }

    $('.info_field').change(function() {
      var _id = getId($(this));  
      var _info = getInfo($(this));
      var _dataString = getInfoString(_id, _info);
      var _url = getUrl(_id);

      var message = "hello text";
      showAlert(message);
    });

    $('.status_yes_field').change(function() {
      var _id = getId($(this));  
      var _info = getInfo($(this));
      var _dataString = getStatusString(_id, _info);
      var _url = getUrl(_id);

      var message = "hello yes";
      showAlert(message);
    });

    $('.status_maybe_field').change(function() {
      var _id = getId($(this));  
      var _info = getInfo($(this));
      var _dataString = getStatusString(_id, _info);
      var _url = getUrl(_id);

      var message = "hello maybe";
      showAlert(message);
    });

    $('.status_no_field').change(function() {
      var _id = getId($(this));  
      var _info = getInfo($(this));
      var _dataString = getStatusString(_id, _info);
      var _url = getUrl(_id);

      var message = "hello no";
      showAlert(message);
    });
  });

I tried to have debugging alerts where I could, but absolutely nothing is registering and I am at a complete loss with low gumption. 我试图在可能的地方安装调试alerts ,但是绝对没有任何注册,而且我全心全意地投入了很多精力。 I am not sure where to go forward with this or even side-step and try something new. 我不确定在此甚至可以避开什么地方继续尝试新的东西。 I can't help but feel like I am missing something very basic here. 我禁不住觉得自己在这里错过了一些非常基本的东西。 Thanks in advance again. 再次感谢。

Welllllll, thanks to only Tejs on this, but this is what I needed. Welllllll,仅感谢Tejs ,但这就是我所需要的。

$(".description_form input, #certificates_holder ul li input").change(function(){

  form = $("#actions form");

$.ajax({data:jQuery.param(form.serializeArray()), dataType:'script', type:'post', url: "/profiles/" + $("#profile").attr("data-profile_id") + "/profile_acts"});

});


$(".additional_info_form textarea").change(function(){

    form = $("#actions form");

$.ajax({data:jQuery.param(form.serializeArray()), dataType:'script', type:'post', url: "/profiles/" + $("#profile").attr("data-profile_id") + "/profile_acts"});

});

You need to attach to the blur / change event of the text box. 您需要附加到文本框的blur / change事件。

 $(document).ready(function()
 {
     $('#myTextBoxId').change(function()
     {
          var currentTextBoxValue = $(this).val();

          $.ajax({
              type: 'POST',
              url: '/Route/To/Your/Ruby/Handler',
              data: { value: currentTextBoxValue },
              success: function()
              {
                   // Insert Code Here when the ajax call comes back successfully.
              }
          });
     });
 });

I'm assuming you have jQuery referenced on your view. 我假设您在视图中引用了jQuery。 You just add the event handler (the call to change() , then get the value (the call to $(this).val() ), and then make the ajax POST to your route. 您只需添加事件处理程序(对change()的调用,然后获取值(对$(this).val()的调用),然后使ajax POST进入您的路由。

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

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