# Link\_to\_remote with href option - syntax help needed

**URL:** <https://www.ruby-forum.com/t/link-to-remote-with-href-option-syntax-help-needed/133766>\
**Category:** Rails\
**Created:** [April 3, 2008, 11:33am UTC](https://www.ruby-forum.com/t/link-to-remote-with-href-option-syntax-help-needed/133766 "2008-04-03T11:33:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [April 3, 2008, 11:33am UTC](https://www.ruby-forum.com/t/link-to-remote-with-href-option-syntax-help-needed/133766/1 "2008-04-03T11:33:47Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [April 3, 2008, 12:13pm UTC](https://www.ruby-forum.com/t/link-to-remote-with-href-option-syntax-help-needed/133766/2 "2008-04-03T12:13:17Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [April 3, 2008, 12:14pm UTC](https://www.ruby-forum.com/t/link-to-remote-with-href-option-syntax-help-needed/133766/3 "2008-04-03T12:14:57Z")

</div>

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

?

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [April 3, 2008, 12:21pm UTC](https://www.ruby-forum.com/t/link-to-remote-with-href-option-syntax-help-needed/133766/4 "2008-04-03T12:21:44Z")

</div>

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 ☹

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [April 3, 2008, 8:46pm UTC](https://www.ruby-forum.com/t/link-to-remote-with-href-option-syntax-help-needed/133766/5 "2008-04-03T20:46:42Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [April 3, 2008, 12:28pm UTC](https://www.ruby-forum.com/t/link-to-remote-with-href-option-syntax-help-needed/133766/6 "2008-04-03T12:28:19Z")

</div>

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…
