how to invoke javascript in the controller?

Hello.

I tried to invoke javascript in the controller, but I couldn't.

What I want to do is, I'd like to verify passed parameters in the controller, if parameters are not verified I'd like to invoke alert with error message and back to othere function.

code will be like below

if verify? params[:value]    #call alert('value is not correct') <- how can I implement this function?    redirect_to :action => 'index'    return end

how can I implement that function? can I insert javascript function before and after of redirect_to action?

and my second question is

<script> var foo = new myclass {   function open() {   } } </script> <%= link_to_remote 'click me', :update=>'blah'. :url=> {:action=>'something'}, :before=>'foo.open();' %>

When I clicked, javascript error occured, "foo is not defined" if javascript is like "alert('blah');" it works. Maybe local and global is the difference while I'm not sure.

anyway, how can I invoke javascript object that is not gloal object?

Hello.

I tried to invoke javascript in the controller, but I couldn't.

What I want to do is, I'd like to verify passed parameters in the controller, if parameters are not verified I'd like to invoke alert with error message and back to othere function.

code will be like below

if verify? params[:value] #call alert('value is not correct') <- how can I implement this function? redirect_to :action => 'index' return end

how can I implement that function?

Unless you're dealing with an ajax request, you can't.

can I insert javascript function before and after of redirect_to action?

and my second question is

<script> var foo = new myclass { function open() { }}

that doesn't look like legal javascript to me.

Fred

Thanks for quick answer

<script> var foo = new myclass {   function open() {   }}

but this was just to write to explain. please don't look grammar itself. My point was legal javascript object didn't invoked.

my real codes are something like below function myclass() {   var open = function() {   } }

var obj1 = new myclass;

and then in the link_to_remote :before=>'obj1.open();' but obj1 is not defined error raised.

and could you explain how to deal with ajax request to implent that insert javascript function before and after of redirect_to action?

thanks.

Thanks for quick answer

> <script> > var foo = new myclass { > function open() { > }}

but this was just to write to explain. please don't look grammar itself.

Well if you're not posting the actual code that's in your app then there isn't much point asking what is wrong with it.

and could you explain how to deal with ajax request to implent that insert javascript function before and after of redirect_to action?

Reading up on RJS is probably a good start (and probably the prototype docs for the Ajax functions (assuming you want to use prototype rather than jquery etc.))

Fred

serenobs:

I am kind nof wondering what you are trying to do. Maybe that is why you are having trouble getting an answer to your questions. I will take a guess at what you are trying to achieve. If this is not helpful, you might want to make your question mor clear.

The reason why you can’t call the javascript in the way that you are doing is because the controller runs on the server, but you are trying to invoke a javascript alert that can only run on the client. It is simply not possible to run code on the client when you are on the server.

Traditionally you would either:

  1. Do the validation on the client, and display the alert when appropriate or
  2. Do the validation on the server, and the response to the client to inform them of the results. Normally this would not be in a javascript, but it could if you like, and it would need to invoked as an onload for the page.

With AJAX, you have a third option 3. Send an asynchronous request to the server to do the validation. Basically javascript is used to send this request, and evaluate the response. Under the appropriate conditions, the javascript can display an alert.

Have you been able to read any books on the topic. There are a number of excellent ones on Rails. There are a number of recommendations of books on this list, if you were to search the archives.

Good luck.