1

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

6
  • Do you know about AJAX? If not, start reading up on it. Commented Apr 7, 2010 at 11:54
  • yes i do know it , can you please let me know to solve this issue , if you know it ? Commented Apr 7, 2010 at 11:56
  • Have you tried sending the variable via AJAX to /posts/get_variable…? Commented Apr 7, 2010 at 11:57
  • deceze i would like to render the variable in same page . Is it possible ? Commented Apr 7, 2010 at 12:41
  • Maybe you should clarify what it is you want first. Also, what's your knowledge of Javascript and AJAX? Are you starting from scratch, or are you just stuck on some specific detail? Commented Apr 7, 2010 at 12:54

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

3 Comments

Salil , it did work but is there way in which i could display the variable in the same page ,Instead of link_to_remote
I need without an link_to_remote not a evented action to display the content.
<%= 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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.