Ruby on rails 如何动态地将路径_更改为()?
我目前有三种方法,我想将它们合并为一种:Ruby on rails 如何动态地将路径_更改为()?,ruby-on-rails,send,Ruby On Rails,Send,我目前有三种方法,我想将它们合并为一种: 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)
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
我想将三个参数折叠成一个,这样我就可以将模型作为参数之一传递,它仍然可以正确地创建路径。我试图用以下方法来实现这一点,但被卡住了:
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的值,不是作为字符串,而是引用对象
我把这件事做好了:——给卡达达打分,因为他给了我正确的方向:)
试试这个:
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
我创建新路径的“手工编码方式”是这样的:new_contact_letter_path(:contact=>contact,:letter=>letter,:status=>'mailed')我查阅了“send”以了解它是如何工作的……你能帮我吗?我更新了我的问题,以包括我所做的“我已经根据你的建议试过了……除了模特儿的名字……谢谢!你帮了我很大的忙,非常棒。
send
方法是对象类上的一种方法。那么我该怎么做呢,我想我无法让你的版本正常工作……我可以再试一次……让它正常工作,但经过修改,但你的帮助起了作用,所以将它授予你:)
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