2010-05-08 101 views
0

目前,我有我要崩潰了爲三種方式:如何動態更改path_to()?

def send_email(contact,email) 

    end 

    def make_call(contact, call) 
    return link_to "Call", new_contact_call_path(:contact => contact, :call => call, :status => 'called') 
    end 

    def make_letter(contact, letter) 
    return link_to "Letter", new_contact_letter_path(:contact => contact, :letter => letter, :status => 'mailed') 
    end 

我想這三個摺疊成一個,這樣我就可以通過模型的參數之一,它仍然可以正確地創建了path_to。我想通過以下要做到這一點,但堅持:

def do_event(contact, call_or_email_or_letter) 
    model_name = call_or_email_or_letter.class.name.tableize.singularize 
    link_to "#{model_name.camelize}", new_contact_#{model_name}_path(contact, call_or_email_or_letter)" 
    end 

感謝的答案在這裏,我曾嘗試以下,這讓我更接近:

link_to("#{model_name.camelize}", send("new_contact_#{model_name}_path", 
             :contact => contact, 
             :status => "done", 
             :model_name => model_name)) 

但我似乎無法弄清楚如何在#{model_name}爲「:」屬性時將它過去,然後將model_name的值發送,而不是字符串,而是引用該對象。

我得到這個工作: - 給點Kadada,因爲他在正確的方向:)

def do_event(contact, call_or_email_or_letter) 
    model_name = call_or_email_or_letter.class.name.tableize.singularize 
    link_to("#{model_name.camelize}", send("new_contact_#{model_name}_path", 
              :contact => contact, 
              :status => 'done', 
              :"#{model_name}" => call_or_email_or_letter))          
    end 

回答

2

讓我試試這個:

def do_event(contact, call_or_email_or_letter) 
    model_name = call_or_email_or_letter.class.name.tableize.singularize 
    link_to("#{model_name.camelize}", send("new_contact_#{model_name}_path", 
       contact, call_or_email_or_letter)) 
end 
+0

您好,感謝 - 我得到一個「有鑰匙?」錯誤? – Angela 2010-05-08 16:08:18

+0

我創建新路徑的「手動編碼的方式」是這樣的:new_contact_letter_path(:contact => contact,:letter => letter,:status =>'郵寄')我擡頭看「send」以瞭解它是如何工作的。 。你能幫我嗎? – Angela 2010-05-08 16:10:34

+0

我更新了我的問題,包括我根據您的建議嘗試了...除了model_name之外的關閉....謝謝!您一直很有幫助,很棒 – Angela 2010-05-08 16:26:55