开发者

dynamically updating html page in rails3

开发者 https://www.devze.com 2023-02-06 16:15 出处:网络
I have an html text-area which uses ajax-autocomplete where it populates the dropdown as i start typing. When I select an entry from the dropdow开发者_StackOverflown it will set some field to the id o

I have an html text-area which uses ajax-autocomplete where it populates the dropdown as i start typing. When I select an entry from the dropdow开发者_StackOverflown it will set some field to the id of that object.

Once I get the id of the object is there a way I could do something like this?

<% @myObjects.find(1) do |myObj| %>
  <h1><%= myObj.attr1 %><h1>
  <h2><%= myObj.attr1 %><h2>
<% end %>

Right now, when I get the id of the object I am using jquery's attr() function to set the values which exposes my javascript logic which I don't really like. Is there a way I can activate the field? Or, hide the fields and where the id is populated show the field and let ruby do its magic with myObjects.find?

UPDATE:

Right now the way I am populating the fields in the view like this:

$(function() {      
  // Executes a callback detecting changes with a frequency of 1 second
  $("#id_element_placeholder").observe_field(1, function(){                                     
    //alert('Change observed! new value: ' + this.value );
    $.ajax({
      type: "GET",
      dataType: "json",
      url: "/myobj/get/" + this.value,
      success: function(data){
        $('#last_name').attr('value', data.myobj.last_name);
        $('#first_name').attr('value', data.myobj.first_name);                  
      }
    });
  });
}); 

Is there a way around exposing the above javascript code?


There is nothing wrong with your javascript and you seem to be confused between client side and server side technologies. Ruby cannot directly update your page once it has been sent to your user. It is on your web server and your page has been sent to the clients browser. It can however send javascript in response to a further request made by your application from the clients browser to do exactly that, just as you are doing.

You could send a new html snippet to your browser and replace the entire node but really there is no point. What you are doing is the same as everybody else who uses javascript.


Answer is simple, you can't :) What you are doing is ok, using AJAX for such things is the way to have such a things done. If you are concerned about exposures don't look at javascript, look at what the server side code allow a user to do with get/post methods ;)

0

精彩评论

暂无评论...
验证码 换一张
取 消