Tag: 发送

如何动态更改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 => […]

Ruby发送方法与rails关联

我一直在搞乱制作可排序的表格模块的事情。 我知道有些可能存在,但我希望自己有这方面的经验。 我有这样想的想法: SortedTable.new(ModelName, Hash_Of_Columns_And_Fields, ID) 例 SortedTable.new(Post, {“Title” => “title”, “Body” => “body”, “Last Comment” => “comment.last.title”}, params[:id]) 我打算做以下事情: def initialize(model, fields, id) data = {} model = model.capitalize.constantize model.find(id) fields.each do |column, field| data[column] = model.send(field) end end 这适用于标题和正文,但是当使用comment.last.title获取Last Comment comment.last.title它会出错。 我已经尝试过Post.send(“comments.last.title”)但是说NoMethodError: undefined method ‘comments.last.title’ for # 我知道我可以做Post.send(“comments”).send(“last”).send(“title”)并且可以工作,但我无法想到如何通过获取字段和分割开来动态地做到这一点。 然后链接发送。 谁能给我建议如何做到这一点? 如果我这样做完全错了,那么请说或指向我做类似的代码的方向。 我不是专家ruby开发人员,但我正在努力。 PS上面的代码可能无法正常工作,因为我不在计算机上使用ruby […]

使用“发送”而不是普通的方法调用有什么意义?

据我了解’发送’方法,这个 some_object.some_method(“im an argument”) 与此相同 some_object.send :some_method, “im an argument” 那么使用’发送’方法有什么意义呢?