0

I have this ruby method:

def something(sometext, jsvariable, rubyobject)
   return HTMLOBJECT
end

And I would like to call this method like this in a javascript function:

$("#someid").append('<% something("sometext", '/jsvar/', rubyobject) %>');

Its not working.I don't know how to call this method from a javascript function with proper formatting of the variables. I don't really know the exact syntax for it.

Any help is much appreciated.

UPDATE: This is what I'm trying to do

function somejsfunc(){
  $.ajax({ 
    url: "/app/SomeController/someMethod",
    data: {'something': something }
    success: function(data){
      var $response1 = $(data);
      jsvar = $response1.find('#anotherselector').text();
      $("#someid").append('<% something("sometext", '/jsvar/', @rubyobject) %>');
    }
 });
}

Please Note

def something 

is a generic method which returns a HTML DOM element based on these parameters,along with other data passed into it.Thanks.

1
  • You will need a route to your method in rails which leads to your something method. In the Ajax-call you have to specify that rout with the desired parameters. These will end up in your controller method in the regular params hash and you would have to continue from there. Commented Jun 19, 2014 at 7:34

2 Answers 2

1

You can not do this, because erb templates compiles before application startup. Use remote requests to take info from server if you want send some js parameters.

UPD: Something like this

$.ajax({
   url: '/some_url',
   data: { jsvar: jsvar },
   success: function(data) {
       $("#someid").append(data.sometext);
   }
});
Sign up to request clarification or add additional context in comments.

3 Comments

Can you please elaborate how??
Actually this is inside an ajax call.The rubyobject is one from the call,and the jsvar has the text of a specified DOM element received from the 'data' of the call request.
Just use this ruby method something on the server.
0

Normally our ruby runs on the server and your JavaScript runs in the browser, so calling one from the other is pretty much impossible. You may of course resort to Ajax as @uselesssmile suggester, but that involves a whole lot more than a simple method call. On the other hand you can compile your ruby to JavaScript using opalrb, then you can package it into your JavaScript on the client and call it directly.

5 Comments

See the thing is,If I call like this: $("#someid").append('<% something("sometext") %>') ;, it is working fine,but it is not recognizing the js and the ruby variable being passed(I'm not sure how to pass it rather!)
And this method is not a controller method.It is defined in a common module.
@Rajarshi: You can call controller methods through appropriate settings in your routes.rb, there you can parse parameters, pass them to your ruby method and package the result to return it. You will have a tough time doing otherwise as long as your ruby runs on your server. As I suggested the other approach is using opalrb to compile your ruby into JavaScript which can be distributed along with your other scripts to the browser. Most plain ruby modules can simple be included into an opal file.
Ok got it.Thanks.Cannot upvote for my rep.Same goes for @uselesssmile.
@Rajarshi: You should be able to accept an answer though, it should do good to your rep. too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.