output link_remote_to in helper

Hi all,

I have a complex link_remote_to link I need to generate often. I want
to DRY up my view and create a helper that generates this
link_remote_to for me.

Is there a way my helper can output the link_remote_to javascript?

Thanks
STijn

I haven't actually tried this, but I'm thinking it should work. Since link_to_remote is just a helper, I don't see why you couldn't create a helper that calls it. Something like:

def my_link_to_remote(<your args>)
  link_to_remote <blah blah blah>
end

It wouldn't be hard to find out.

Peace,
Phillip

nope, tried that but that doesn't work.

I'll have to put it in a partial I think.

Regards,
Stijn

You might want to do some more research then. I just did a simple test like this:

In a view template

<%= my_link_to_remote 'Click here', 'wgg', 'link_to_test', 'link_to_target' %>
<div id="link_to_target">
</div>

in the helper

def my_link_to_remote(label, controller, action, update)
  link_to_remote label, :url => {:controller => controller, :action => action}, :update => update
end

and in the controller

def link_to_test
  if request.xhr?
    render :text => Time.now.to_s
  end
end

And it worked as I expected it to. You should be able to pass in whatever args you need and call link_to_remote to build the link for you. Unless your need is extremely complex. Maybe you can post some example of what you're trying to do?

Peace,
Phillip

I got it to work. The link_to_remote indeed just works in the helper.

I have an additional problem though:
I want to output 2 link_to_remotes to my view with the helper:

I have something like
if(condition1)
link_to_remote1
link_to_remote2
else
link_to_remote1
link_to_remote2
end

It always just outputs 1 linkto_remote (the second). I'm I doing
something wrong?

regards,
Stijn

You are experiencing one of the niceties of Ruby: the last "thing" in a method gets returned.

You will want to capture the output of both calls to link_to_remote and return them together. I have *not* tried this, but somethiing like this should work

link_string = ''

link_string = link_to_remote blahblahblah
link_string += <something to separate the links, maybe a <br /> ?>
link_string += link_to_remote blahblahblah

return link_to_string

Peace,
Phillip