2012-06-19 11 views
5

मैं हाल ही में Haml के लिए ERB से मेरे टेम्पलेट के कुछ बदल दिया। ज्यादातर यह साफ और अच्छी तरह से बन गया, लेकिन बटन परिभाषाओं को चूसना शुरू कर दिया।मार्ग पथ जाओ गतिशील

मैं इस

= new_button Intern 

की तरह कुछ करने के लिए इस

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

कन्वर्ट करने के लिए मैं 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

संबंधित मुद्दे