0

HAMLヘルパーを使用してメニューを描画しています

module ApplicationHelper
  def draw_menu
    items = %w[
      item1
      item2
    ] # community

    capture_haml do
      haml_tag :nav, :class => "main-menu" do
        haml_tag :ul do
          items.each do |item|
            css_class = nil
            css_class = "first-item" unless item != items.first
            css_class = "last-item" unless item != items.last

            haml_tag :li, :class => css_class do
              haml_concat link_to t("nav.#{item}")
            end
          end
        end
      end

      haml_tag :div, :class => "clearize"
    end
  end
end

私はそれをテストしようとしています(期待される出力と一致させたいです/<nav class='main-menu'>\s*<\/nav><div class='clearize'><\/div>/

私が実際にしたことは

require "spec_helper"

describe ApplicationHelper do

  before(:all) do
    init_haml_helpers
  end

  it "should draw a menu" do
    draw_menu().should_not be_empty
  end

end

ただし、次のエラーが発生し続けます。

1) ApplicationHelper should draw a menu
   Failure/Error: draw_menu().should_not be_empty
   ActionController::RoutingError:
     No route matches {}
   # ./app/helpers/application_helper.rb:20:in `block (5 levels) in draw_menu'
   # ./app/helpers/application_helper.rb:19:in `block (4 levels) in draw_menu'
   # ./app/helpers/application_helper.rb:14:in `each'
   # ./app/helpers/application_helper.rb:14:in `block (3 levels) in draw_menu'
   # ./app/helpers/application_helper.rb:13:in `block (2 levels) in draw_menu'
   # ./app/helpers/application_helper.rb:12:in `block in draw_menu'
   # ./app/helpers/application_helper.rb:11:in `draw_menu'
   # ./spec/helpers/application_helper_spec.rb:10:in `block (2 levels) in <top (required)>'

なんで? どうすれば期待どおりに機能させることができますか?

ありがとう!

4

1 に答える 1

0

link_toその引数として文字列だけを渡すことを呼び出すと、その文字列がリンクテキストとして使用されます。リンクのターゲットは、現在のアクションとコントローラーによって指定されます:(url_for基になるヘルパー)は、指定したパラメーターを現在のアクションを構成するパラメーターとマージするだけです。これが理由です

link_to 'something', :action => :an_action

an_actionビューをレンダリングするコントローラーのメソッドへのリンクを生成します。

ヘルパー仕様では、この環境は存在しません。現在のアクションまたはコントローラーがないため、これは機能しません。

于 2012-07-16T08:46:22.487 に答える