2012-06-19 13 views
5

最近私のテンプレートの一部がERBからHamlに変換されました。主に、よりクリーンで上手くなりましたが、ボタンの定義が吸い始めました。経路を動的に取得する

私は私が他のすべてのページが同様にこの恩恵を受けるInternのほか、いくつかの他のエンティティを持っているこの

= new_button Intern 

のようなものに、この

= link_to t('.new', :default => t("helpers.links.new")), 
      new_intern_path,          
      :class => 'btn btn-primary' if can? :create, Intern  

を変換したいです。

だから、私は期待どおりに働いて、このコード

def new_button(person_class) 
    return unless can?(:create, person_class) 

    new_route_method = eval("new_#{person_class.name.tableize}_path") 

    link_to t('.new', :default => t("helpers.links.new")), 
       new_route_method,          
       :class => 'btn btn-primary' 
    end 

を入力しました。私はちょうどそれについてeval呼び出し(それは悪いので、すべて)であるか分からない。もっとシンプルで悪い方法はありますか?

def edit_button(person) 
    return unless can?(:edit, person) 

    link_to t('.edit', :default => t("helpers.links.edit")), 
      send("edit_#{person.class.name.singularize.underscore}_path", person), 
      :class => 'btn btn-mini' 
end 

答えて

6

ああ、ここでの改良版です。それによって

、あなたのコードのようなものになります:

うれしい
def edit_button(person) 
    return unless can?(:edit, person) 

    link_to t('.edit', :default => t("helpers.links.edit")), 
      edit_polymorphic_path(person), 
      :class => 'btn btn-mini' 
end 
+0

を[はい、それははるかに優れていることに – Viren

関連する問題