MJ wrote:
Hi,
Is it possible to call a Rails controller from a javascript function?
This is part of my code: <%= start_form_tag({:action=> "index"}, {:onsubmit => "clickResult(); return false;", :autocomplete => 'off'}%>
The clickResult() function: function clickResult() { window.location = "http://myblog.com/post/" + document.getElementById("Highlight").firstChild.id; }
I want to call this function from javascript because now I have to hard-code the url: def permalink @post = Post.find(params[:id]) end
Any ideas? MJ
This might work, I haven't tested it at all, but it seems like it should do the trick... The main addition is the 'remote_function' action, which performs a Ajax request to the given parameters
Cheers, Gustav
<%= start_form_tag({:action=> "index"}, {:onsubmit => update_page{|page| page.call('clickResult') remote_function(:url => {:controller => 'some_controller), :action => 'permalink', :id => post.id})}, :autocomplete => 'off'})%>
The clickResult() function: function clickResult() { window.location = "http://myblog.com/post/" + document.getElementById("Highlight").firstChild.id; }