如何使用动态类的 .send 方法转到 Rails 中的特定路径?

发布于 2024-09-26 10:21:12 字数 407 浏览 8 评论 0原文

我正在使用以下内容:

send("#{done_event.class.name.tableize}_path", done_event.id)

一个例子是done_event是ContactEmail的特定实例。

我希望它代表路径 contact_email_path(done_event.id) ,它将转换为类似于 contact_emails/1 的内容

但是,我得到的结果是 contact_emails.1

不确定该怎么办...?

当我尝试传入对象时,我也会收到此错误:

http://localhost:3000/contact_calls.%23%3Ccontactcall:0x9fefb80%3E

I am using the following:

send("#{done_event.class.name.tableize}_path", done_event.id)

An example is done_event is a specific instance of ContactEmail.

I would like this to represent the path contact_email_path(done_event.id) which would translate to something like contact_emails/1

However, the result I get is contact_emails.1

Not sure what to do...?

I also get this error when I just try to pass in the object:

http://localhost:3000/contact_calls.%23%3Ccontactcall:0x9fefb80%3E

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

惯饮孤独 2024-10-03 10:21:12

这似乎可以解决问题:

send("#{done_event.class.name.tableize.singularize}_path", done_event)

虽然如果多态有效,我想使用它。

This seems to do the trick:

send("#{done_event.class.name.tableize.singularize}_path", done_event)

although if the polymorphic worked, i would like to use that.

画骨成沙 2024-10-03 10:21:12

也许是这样的:

eval("#{done_event.class.to_s.underscore}_path(done_event)")

Maybe something like:

eval("#{done_event.class.to_s.underscore}_path(done_event)")
白云不回头 2024-10-03 10:21:12

尝试 polymorphic_url 像这样

polymorphic_url([:contact_emails, done_event])

编辑:因为你不知道 'done_event' 的类,你所要做的就是像这样通过。

polymorphic_url(done_event)

Try polymorphic_url like this

polymorphic_url([:contact_emails, done_event])

EDIT: Since you don't know the class of 'done_event', all you have to do is pass like this.

polymorphic_url(done_event)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文