Calling a controller from javascript function

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

MJ wrote:

window.location = “http://myblog.com/post/” +
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;
}

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’})%>

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs