开发者

link_to_remote does not generate correct url in Haml

In Haml, I've been trying to get the following link_to_remote call to work. It's called from the /questions/new view.

#{link_to_remote image_tag('x.png'), :url => {:controller => 'questions', :action => 'remove_tag_from_cart'}}

I've tried the followi开发者_StackOverflow中文版ng variations.

#{link_to_remote image_tag('x.png'), :url => {:controller => :questions, :action => :remove_tag_from_cart}}
#{link_to_remote image_tag('x.png'), :controller => 'questions', :action => 'remove_tag_from_cart'}
#{link_to_remote image_tag('x.png'), :controller => :questions, :action => :remove_tag_from_cart}

In every case, I get the following link: /questions/new#. I'm not sure why!

I also have the following in routes.rb, thinking that was the problem...

map.connect ':controller/remove_tag_from_cart', :action => 'remove_tag_from_cart'


map.connect ':controller/remove_tag_from_cart', :controller=>:controller , :action => 'remove_tag_from_cart'

Restart server and check.

EDITED

link_to_remote use for the ajax request. so when you hover the link you will get your url append with # but when you click on it it call the ajax request to a url provided by you. i explain it below.

<%= link_to_remote image_tag('x.png'), :url => {:controller => 'questions', :action => 'remove_tag_from_cart'}} %>

when you check generated html using it you will get

<a onclick="new Ajax.Request('/questions/remove_tag_from_cart', {asynchronous:true, evalScripts:true}); return false;" href="#">
   <img src="/images/x.png" alt="X"/>
 </a> 

you can see href="#" in your generated html

Note:if you don't want it either you have to use link_to.


if /questions/new# is what shown in your status bar in browser then check page source again because link_to_remote generates javascript ajax request and actual link is hidden inside onclick attribute

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜