I have 2 text fields. One is for the input of a member's id, and the other is the output of the member's name.
When I type a member's id in the first field and the action onblur is triggered, I would like to show the member's name in the other field.
This is my Javascript function:
function show_name(){
id = document.getElementById('id_member').value;
field_name = document.getElementById("name_member");
field_name.value = "<%= Member.find().nom %>";
}
In the method Member.find() I would like to send the value of the variable id that I've declared at the beginning. Example:
field_name.value = "<%= Member.find(*Javascript "ID" variable value*).nom %>";
How can I insert that value into my Ruby injection?
Thanks.
Javascript is run on the client, ruby is run on the server. You can't pass a javascript variable into the ruby code, unless you make a new request to the server.
Your options are to reload the whole page with a new query, use ajax to make a new query and do something with the result, or to download all possible Members into the page ahead of time and have javascript pull it out of the page somehow.
Related
Given the id of an element I need to make a query in javascript, for example:
javascript:
var post_value = $(".post").val() # return a post ID;
var post = #{Post.find_by(id: post_value).to_json.html_safe};
But the post_value variable don't recognize it
If I manually pass a value to it, it recognizes it and asks me the query of the post, for example:
var post = #{Post.find_by(id: 181).to_json.html_safe}; # If work because I pass 181 as an ID value
I would be very grateful if you could help me
This line gets rendered in server side. It will not have any access to the browser's runtime context. As this gets executed before reaching the user.
var post = #{Post.find_by(id: post_value).to_json.html_safe};
Either use XHR (have another endpoint API purely for data objects like json) or query your Post object/s in controller, pass the object to the view and render the Post object inside the view. It is also a bad practice to have DB queries inside the view template files.
It's also weird to use javascript to render elements in the backend context. You generally use javascript with the context or assumption that it is to be executed in the user's side, separated with the backend context.
I have in php getting id from server request for parameter id
$id = $_REQUEST['id'];
$smarty->assign('id', $id);
And in the smarty .tpl display files {$id} outputs the number to the client browser.
I need this variable in the javascript for the page:
var id = {$id};
I use id later in the javascript to define datatables default search value.
"oSearch": {"sSearch": offer_id},
My problem is I need to properly pass the id to the javascript, if is set at all. Right now it displays {$id}. Any ideas, please?
In your template (.tpl) file, include the code
<script>
var id = {$id};
console.log(id); // for debugging purposes
</script>
In your browser's Javascript console, you should be able to see the value of id.
I receive a google ID from an Ajax call and then want to use that ID to update a button on my view.
Here is the code in new.js.erb, which is linked to a new.html.erb.
Problem is, I don't know how to pass the variable's content. Restaurant is a json. The alert returns the correct ID and when I search my db on the terminal with the returned google id I find the restaurant.
Here is the code:
alert(restaurant["google_id"]);
var google_id = restaurant["google_id"];
$("#rating_bar").html("<%= escape_javascript(render 'reviews/buttons/full_profile_rate_restaurant', :google_id => "+google_id+".html_safe) %>");
What happens is that the variable being passed is the string "google_id" instead of the combination of letters and numbers that is the google ID. I've tried multiple approaches, this is just one of many wrong one - I think this question is pretty easy for anyone who knows their JS really well.
It is not possible to pass a JS variable to the ruby partial.
As Ryan Bigg explained for the same type of problem here, its not possible to send the variable while rendering that partial. We need to work out some thing else. Even i also had the same issue once.
Alternatively,
if that is google_id is only a variable to display in the partial, then update those divs manually after rendering that partial.
like
$("#rating_bar").html("<%= escape_javascript(render 'reviews/buttons/full_profile_rate_restaurant', :google_id => "sample_id") %>");
// Now update the required elements
$("#what-ever-ids").text(google_id);
or just create some other action in that controller, and call send an ajax request to that action, and there you will have this js variable, and in that js.erb file render the same partial which you actually want to update with the google_id variable.
I am using Rails and jquery, and I need a dynamic content with ajax.
But I don't know how to get the current user ID
For example the url is www.mywebsite.com/users/20
In my javascript file I need the user ID (20)
$.get("/users/<%= **20** %>.json", function(data)
{
}, "json");
Thanks in advance for any help.
Is there another way to do this ?
Generally i put a hidden field with the id somewhere in the page, where i can easily access with $("#user_id") on the javascript file, then the request would be something like this:
var id = $("#user_id").val();
$.get("/users/"+id+".json", function(data) { }, "json");
You can assign the rails variable value to a javascript variable in your view file like
"<%= javascript_tag "var userId = #{#current_user.id};" %>"
and access the variable inside js file wherever you want.
$.get("/users/<%= params[:id] %>.json", function(data)
{
}, "json");
Should do the trick.
Include the snippet into the .erb file or create a .js.erb partial with this.
Lots of choices:
If you have a current user object with the id (Note: this must be in a file ending with .js.erb):
$.get("/users/<%= #current_user.id %>.json", function(data){...}, "json");
If you don't want to use a .js.erb file, but still need the value to be set somewhere on the page, then set a data-attribute or hidden input with the id value.
If you have the id value in the url (as in the current url you are on looks like www.mywebsite.com/users/20 and you want the GET request to use 20 as the value), you can use document.url or window.location.href and some string manipulation to get the id.
Try
<%= escape_javascript(params[:id]) %>
I'm not completely sure if escape_javascript is necessary or will help.
I have a page that I have a page I pull from the server every x seconds using some ajax, and then I replace some HTML on the site with the new HTML pulled from the server. The problem has always been that there is a form in that HTML. I want to know is there a way to preserve the value of the form (that the user has entered) when replacing the html in javascript.
Use two callback functions (you should use $.ajax), in the callback before sending (beforeSend(x){ /your code here/; }) you save the parameters (to an array or hashtable): saved = $(element).val(); then in the second callback (use success(x){}) you write them back in. using $(element).val(saved);
var save = document.getElementById('userForm').value;
//replace HTML
document.getElementById('userForm').value = save;
Two ways,
Send and then replace the value in the HTML on the server
Using JavaScript, save it in a session: http://www.webreference.com/authoring/languages/html/HTML5-Client-Side/
I'd say the best solution would be to combine the two and save a session on the server, then load it each time you load the HTML.
-Sunjay03