0

この haml コードを変換して、リストではなくメニュー項目のドロップダウンを表示するにはどうすればよいですか?

Rails 4.0 アプリ内の既存のコード:

アプリ/ビュー/アプリケーション/_archive.html.haml

.sidebar-header=I18n.t('blog.archive')
-archive_dates.each do |date|
    .archive-link=archive_link(date, '%B %Y')

アプリ/モデル/post.rb

def self.archive_dates(audience)
    archive_dates = Array.new
    published.send(audience).each do |post|
      archive_dates << post.publish_date.to_date if archive_dates.count == 0 || post.publish_date.month != archive_dates.last.month || post.publish_date.year != archive_dates.last.year
    end
    archive_dates
end

アプリ/ヘルパー/application_helper.rb:

def archive_link(archive_date, format)
    archive_date = archive_date.to_date
    link_to I18n.l(archive_date, format: format), posts_path(month: archive_date.month, year: archive_date.year)
  end

これは出力 HTML です。

http://i.stack.imgur.com/bFaxf.png

ユーザーは月を選択して、その月に作成されたすべての投稿を表示できます。同じ形式と動作を探していますが、代わりにドロップダウン メニューを使用する必要があります。

4

2 に答える 2

0

これにより、ラベルが書式設定されたリンク タグとして表示され、値が実際のリンクとして表示されるドロップダウン メニューが表示されます。

= select_tag "name_of_tag" , options_for_select( archive_dates.map{ |date| 
                              [archive_link(date, '%B %Y').html_safe, #label
                              posts_path(month: date.month, year: date.year),  #value
                              :class => "archive_date" ] # class of each option
                            }), onchange: "window.location = this.options[this.selectedIndex].value;" 

html_safe生の HTML として表示するには、ラベルごとに使用する必要があります。

html オプションを追加しonchangeて、ユーザーが選択されたオプションにいつでも移動できるようにしました。

于 2013-09-17T23:19:09.790 に答える