views:

415

answers:

1

Hi

Inside my index.html.erb

<script type= "text/javascript">
  var msg = "Hello World" ;
</script>

i need pass this var msg to my controller say get_variable() method in my Post controller.

Edit : get javascript variable msg in same index.html.erb as ruby variable Thanks in Advance

+1  A: 

Try this

<%= link_to_remote, 'Message', :url=>{:controller=>'controller_name', :action=>'method_name'}, :with=> "'msg='+msg" %>

controller_name.rb

def method_name
  @message= params[:msg]
  puts @message # >> should be print "Hello World"

end

EDITED

<%= link_to_remote, 'Message', :url=>{:controller=>'controller_name', :action=>'method_name'}, :with=> "'msg='+msg" %>
<div id ='show_message'></div>

controller_name.rb

def method_name
  @message= params[:msg]
  render :update do|page|
    page.replace_html 'show_message', @message
  end
end
Salil
Salil , it did work but is there way in which i could display the variable in the same page ,Instead of link_to_remote
Big Bang Theory
please check my edited answer.
Salil
I need without an link_to_remote not a evented action to display the content.
Big Bang Theory
<%= periodically_call_remote ,:url=>{:controller=>'controller_name', :action=>'method_name'}, :with=> "'msg='+msg" , :frequency => '20' %>but this is not proper way as after every 20 sec an ajax request is sent to the server
Salil