开发者

Rails - OK to set a Rails variable inside a javascript function?

开发者 https://www.devze.com 2023-02-17 14:21 出处:网络
Is there anything wrong with setting a Rails variable inside a javascript function?I haven\'t been able to google a definete answer and just wanted to make sure there\'s not a catch or flaw I\'m unawa

Is there anything wrong with setting a Rails variable inside a javascript function? I haven't been able to google a definete answer and just wanted to make sure there's not a catch or flaw I'm unaware of. I'm actually confused as to how this works at all - if javascript is executed on the client side, I wouldn't have assume Rails would respond to a variable change in the view after the view is rendered..?

(example) I have multiple functions calling the same form and I have a need to know which function called the forms to do specific tasks in the controller开发者_如何学Go method (session in this case is a cookie, but I also tried a local Rails variable and it works as well).

<script type="text/javascript">

    $('.open_new_item_form' ).click(function(){
     alert('<%= session[:item_form_type]%>');
        <% session[:item_form_type] = "from_new_item_click" %>
     alert('<%= session[:item_form_type]%>');
        var url = $(this).attr("href");
        $.ajax({
etc...

The alerts seem to indicate it works well. It will alert with the current valute and then the new "from_new_item" value after the set.

Thanks - just making sure


Your erb file is being interpreted completely on the server; this line will execute as part of rendering the javascript, not when the javascript is executed on the client:

    <% session[:item_form_type] = "from_new_item_click" %>

If you place this line of code within a javascript if block that evaluates to false, your server-side variables will still be changed:

    if(0){<% session[:item_form_type] = "from_new_item_click" %>;}

(Forgive my pidgin-javascript, it isn't a language I speak.)


Part of the rendering process for rails is to evaluate all the erb code within that view. Once the evaluation of the code is completed, rails will then render the file(whether it is javascript, html, etc).

So when you say that you were surprised to see that rails could respond to a variable change, it's because the view wasn't actually rendered at that point... it was in the process of rendering.


As the others said, your view is first rendered at the server and then sent to the browser. By the time it gets to the browser, all of your Ruby code has long since been executed and the Javascript you see at the browser is the result of that. Try looking showing the source of the page from your browser; you should see that the line:

alert('<%= session[:item_form_type]%>');

has already been replaced, without any clicks, with:

alert('from_new_item_click');

If you really do want to change your server-side RoR code when the user takes an action, you'll need to use AJAX. Basically, AJAX lets your Javascript code send a GET/POST to a URL. So, within your .click event handler, you can hit a URL that calls an action that changes whatever session variables you want.

(Sorry if you know all about AJAX already; I suspect you might not, though, given your question.)

0

精彩评论

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