2010-05-08 71 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