给一个haml链接一个id

时间:2013-04-12 19:33:31

标签: ruby-on-rails haml

我有以下代码,但是如何设置它以使链接的id标记为*“title_header”* 像这样

<a href="/movies?sort=title" id="title_header">Movie Title</a>
%th{:class => @title_header}= link_to 'Movie Title', :sort => "title"

我尝试了这个,但它没有用

%th{:class => @title_header}= link_to 'Movie Title', :sort => "title", :id => "title_header"

它给了我这个

<a href="/movies?id=title_header&amp;sort=title">Movie Title</a>

2 个答案:

答案 0 :(得分:2)

为什么不使用hash(#)来设置Id?

%th{:class => @title_header}
%a#title_header{:href => "/movies?sort=title"} Movie Title

答案 1 :(得分:0)

你接近第二个例子:

  %th{:class => @title_header}= link_to 'Movie Title', :sort => "title",
      :id => "title_header"

应该是:

  %th{:class => @title_header}= link_to "Movie Title",
      movies_path({:sort => 'title'}), {:id => 'title_header'}

指定网址时不要使用文字(如您接受的答案所示);当应用程序稍后更改时,您只需为自己创建工作。