开发者

Syntax for link_to with block in rails3 with :remote=>true and including :class and :id

For the love of god, I've been banging my head on this for hours. Using rails3 rc, 1.9.2.

I'm trying to create a link_to that submits an ajax request, with parameters, a class and id, and needs a block so I can insert a span tag around the name. Documentation is of absolutely zero help, as are numerous google searches. Here's what I've got so far:

<%= link_to(
      :url=>{
        :controller => 'themes', :action => 'remove_tag',
        :entity_id => entity_id, :theme_id => theme_id,
        :entity => entity, :element_id => element_id, :parent_id=>parent_id
      },
      :remote => true,
      :id => "theme-tag-#{entity}-#{entity_id}",
      :class => "tag")  do %&g开发者_如何学JAVAt;
  <span class='subtract'><%= tag %></span>
<% end %>

The generated url looks like this:

<a href="/explore/index/theme-tag-user-3?url[controller]=themes&amp;url[action]=remove_tag&amp;url[entity_id]=3&amp;url[theme_id]=16&amp;url[entity]=user&amp;url[element_id]=filter-contributor-3&amp;url[parent_id]=filter-contributors&amp;remote=true&amp;class=tag">

Test descriptor

I can't indicate properly that the text "Test descriptor" is actually properly included within the span; code formatting is failing a little here, however, the href is wrong, there's no class or id, and downhill it continues to roll

If I didn't need the block, I could just add the name and not have to specify :url=>{...} (leaving if off throws an exception with the block, go figure) and then follow that with :remote=>true, :id=>"whatever", :class=>"blah" and it works. What am I doing wrong? Because I'm new to rails in general, I'd also like to understand why this syntax must differ so much? I mean really, thank god you don't have to write a lot of links like this in a web app... ;-)

Thanks in advance


turns out you have to do url_for(...) instead of :url=>{...} and it all worked as expected.


Just putting wkhatch's comment here so it's nicely formatted.

<%= link_to(
    url_for(:controller=>'themes', 
            :action=>'remove_tag', 
            :entity_id=>entity_id, 
            :theme_id=>theme_id, 
            :entity=>entity, 
            :element_id=>element_id, 
            :parent_id=>parent_id), 
    :remote=>true, 
    :id=>"theme-tag-#{entity}-#{entity_id}") do %>
       <span class='subtract'></span><%= tag %>
<% end %>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜