2013-03-15 80 views
-2

我有一个文本字段输入,它包含数据库属性(ref)的值。正如您从代码中注意到的那样,当我将注意力集中在文本字段上时,边框将出现,并且在单击时边框将会消失。在rails上使用ajax或jQuery更新数据库

我的问题是,当我点击时,我希望文本字段中的数据将被存储在没有提交按钮的数据库中。

<script> 
$(document).ready(function(e){ 

    $('.class1').focusin(function(){ 
     $(this).attr('readonly',false); 
     $(this).css('border','1px black solid'); 
    }) 

    $('.class1').focusout(function(){ 
     $(this).attr('readonly',true); 
     $(this).css('border','0px white solid'); 

    }) 
}); 
</script> 

<div>Ref: <%= text_field_tag(:ref,@ref.to_s,:readonly=>true, :class => "classe1")%> </div> 

如何将值发送到我的应用程序?

+0

http://guides.rubyonrails.org/ajax_on_rails.html – DickieBoy 2013-03-15 15:20:46

回答

0

感谢所有您的帮助。
这个问题的解决方案是使用JavaScript函数,将在控制器中调用其他功能

function loadXMLDoc(id, val, name) 
{ 

    var xmlhttp; 
    if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
     xmlhttp=new XMLHttpRequest(); 
    } 
    else 
    {// code for IE6, IE5 
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.onreadystatechange=function() 
    { 
     if (xmlhttp.readyState==4 && xmlhttp.status==200) 
     { 
      //document.getElementById("myDiv").innerHTML=xmlhttp.responseText; 
      alert(); 
     } 
    } 

      //update_ref_field => methode in the contoller with complete the update in the data  base 
      xmlhttp.open("GET","update_ref_field?ref1="+val ,true); 
      xmlhttp.send(); 


} 
0

发送ajax请求:http://api.jquery.com/jQuery.ajax/。如果记录存在并且您正在更新,请将“类型”设置为“PUT”;如果记录是新记录,则将其设置为“POST”。发送此信息的网址很可能是我认为您提供的控制器的名称。例如“/控制器”。

例子:

$.ajax({ 
    type: "POST", 
    url: "/controller", 
    data: {ref: $('.classe1').html().trim()} 
}); 

如果你不能得到它的工作,你需要提供有关您正在使用的Rails代码一些更多的信息,我拍了几张普通的猜测。

1

为了更新特定记录,您只需使用jquery为该输入字段或文本框调用blur函数。使用输入字段的类名称或ID调用模糊函数。

$('#ref').blur(function() { 
    update_ref_field(); 
}); 

JS函数来调用AJAX:

function update_ref_field(){ 
    var ref_value = $('#ref').val(); 
    var url = '/controller_name/action_name/?ref='+ref_value; 
    $.ajax({ 
     type: 'put', 
     url: url, 
     dataType: "jsonp", 
     // You can use this jsonp if your request related to cross domain 
     error: function (result, status, xhr){ 
      alert('result='+result+'::status='+status+'::xhr= '+ xhr); 
      alert('Error occurred while updating the record.');   }, 
     success: function(result, status, xhr){ 
      alert('result='+result+'::status='+status+'::xhr= '+ xhr); 
      alert('Record updated successfully.'); 

    }); 
    return false; 
} 

更新的代码

function update_ref_field(){ 
    var ref_value = $('#ref').val(); 
    var url = '/controller_name/action_name/?ref='+ref_value; 
    $.ajax({ 
    type: 'post', 
    url: url, 
    dataType: 'script', 
    error: function(data){ 
     alert('Error occurred while updating the record.'); 
    }, 
    success: function(data){ 
     alert('Record updated successfully.'); 
    }); 
    return false; 
} 
+0

感谢您的帮助 但它确实不工作: 此消息已在提醒上显示: result = [object Object] :: status = error :: xhr =未找到 您知道我该怎么做。再次感谢 – 2013-03-18 15:15:22

+0

我已经写了我的代码在aswer,请你能找到错误。 – 2013-03-18 15:47:04

+0

请参阅最新的代码。 – Rubyist 2013-03-19 03:45:10

0

,这是我的代码:
的观点:

<script> 
    function update_ref_field(){ 
    var ref_value = $('#ref1').val(); 
    var url = '/ModController/update_ref_field/?ref1='+ref_value; 
    $.ajax({ 
     type: "POST", 
     url: url, 
     //dataType: "jsonp", 
     // You can use this jsonp if your request related to cross domain 
     error: function (result, status, xhr){ 
      alert('result='+result+'::status='+status+'::xhr= '+ xhr); 
      alert('Error occurred while updating the record.');   }, 
     success: function(result, status, xhr){ 
      alert('result='+result+'::status='+status+'::xhr= '+ xhr); 
      alert('Record updated successfully.'); 
     }}); 
     return false; 
     } 


     $('#ref1').focusin(function(){ 
      $(this).attr('readonly',false); 
      $(this).css('border','1px black solid'); 
     }) 

     $('#ref1').focusout(function(){ 
      $(this).attr('readonly',true); 
      $(this).css('border','0px white solid'); 
      $javascript:update_ref_field(); 


    }) 
}); 
DEF update_ref_field()
开始
@ projj = Project.find。(PARAMS [::PROJECT_ID])#projet柯朗
@local_date = Time.new()TO_DATE

控制器上


@sem = caluculer_semaine(@local_date)
@existe_mom_pour_cet_semaine = Mod.find(:所有,:条件=> {:PROJECT_ID => @ projj.id,:semaine => @sem})
#CE CAS对应联合国更新
开始
@ existe_mom_pour_cet_semaine.each do | a |
if(params [:ref1])!=“”|| (params [:ref1])!=无
a.update_attributes(:ref => params [:ref1])
end
if a。保存
闪光[:通知] = “濑一个元旦REF”
别的
闪光[:错误] = “濑ajour FR REF非aboutit”


救援异常=>电子
把e.message
放e.backtrace.inspect

redirect_to的:行动=> '团聚'


当我使用tag_form

,我正常工作,但我想,关于数据库的变化会在后台
感谢

+0

感谢evry one,我找到了解决方案:) – 2013-03-19 07:44:50