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
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---
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; } --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Sorry for the syntax error!
here''s the fix
<%= 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''})%>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---