Link_to_remote with href option - syntax help needed

Hi

I’m using link_to_remote, in a page that (as usual) needs to work for
non-js enabled browsers.

According to the API
-(http://www.railsbrain.com/api/rails-2.0.2/doc/index.html?a=M001827&name=link_to_remote),
i can pass an href option which points to a url which is called in the
non-js version.

In this case, i want the ajax call and the normal html link to go to
the same controller and the same action:
“selections/create?resource_id=x”.

My problem is that i’m using restful routes and can’t seem to work out
the syntax. Here’s what i thought it should be:

link_to_remote image_tag("/images/v4/basket.gif", :alt => “Add to
basket”),
{ :url => selections_path(:resource_id => resource.id), :method =>
:post }, :href => url_for( :controller => “selections”, :action =>
“create”, :resource_id => resource.id )

This works in the js case: the restful route of calling selections_path
with a POST request sends me to selections/create.

But the href option doesn’t work properly: it calls
“selections/index?resource_id=x”

Can i pass a restful route through to href as well? I’ve tried various
arrangements but it keeps breaking because of bad syntax. I have a
feeling that if i’m using a restful route helper i can drop url_for and
just point href at the helper but i can’t work the syntax out for that
either.

Can anyone help?
thanks
max

The :url syntax seems to be correct.
BTW, href doesn’t support RESTful create action since create need to
be POST.

On Apr 3, 5:33 pm, Max W. [email protected]

I’m using link_to_remote, in a page that (as usual) needs to work for
non-js enabled browsers.

You could play with format.html. That should be called when JS is not
enabled.
something like…

format.html { redirect_to(resources_path) }

?

Vapor … wrote:

I’m using link_to_remote, in a page that (as usual) needs to work for
non-js enabled browsers.

You could play with format.html. That should be called when JS is not
enabled.
something like…

format.html { redirect_to(resources_path) }

?

Hi Vapor

I have my formats set up fine, like you suggest: the problem is that it
doesn’t reach them because it gets sent to the wrong controller action
(index instead of create) in the html case.

All i need is a link that sends an ajax request or an html request to
the same action and the same controller, depending on whether the user
has js enabled. This must be a very common requirement for any site,
which is why i’m going so crazy trying to work it out :frowning:

Try specifically setting :html => {:url => …}

I’m not being RESTful, but this is working for me:

(link_to_remote ‘«prev’,
:url => {:action => ‘change_month’, :month =>
@date.last_month.month, :year => @date.last_month.year},
:html => {:href => url_for(:month =>
@date.last_month.month, :year => @date.last_month.year), :class =>
“otherMonthLink”})

Sorry that the lines are so long!

-Kyle

On Apr 3, 5:28 am, Max W. [email protected]

Hi Ashchan

The :url syntax seems to be correct.
hmmmm…in that case, can you see any other reason why it might request
the index action instead of the create action.

BTW, href doesn’t support RESTful create action since create need to
be POST.
I wasn’t sure about href, that’s why i explicity passed through the
controller and action. It just seems to ignore the action though. I
have a feeling this is just to do with arranging my {}s and ()
properly…

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