Rails 中上下文相关模型默认值(来自 link_to)

发布于 2024-08-19 00:24:20 字数 1119 浏览 3 评论 0原文

从链接向 Rails 模型应用默认值的标准是什么?情况是这样的:

Spree 中有一个名为 ProductGroup 的模型,它是定义如何查找 Product 的设置集合(基于您设置的一堆属性值)。我希望能够使用此结构来定义常见的产品组:

  • 活动产品
  • 可订阅产品
  • 门票
  • 捐赠 ...

因此,如果我在“创建活动”页面上,并且用户单击“创建门票”,我希望这会将他们带到“产品组”页面,以便他们可以为该活动创建一个新的产品组,说“我的酷”活动产品”。

我想尽可能简单地做到这一点,而不必覆盖/扩展控制器/模型。我正在考虑使用的解决方案是将 :defaults 哈希从视图中的 link_to 方法传递到控制器,如下所示:

<%= link_to "创建与活动相关的产品", new_admin_product_group_url(:defaults => {:name => "活动名称和日期: " + Time.now.to_s}) %>

这样我就可以写一个通用插件,基本上只将控制器中的 params[:defaults] 内的任何属性应用于模型。

那里有类似的东西吗?或者是否有更好/推荐的方法来执行此操作,也许可以定义自定义 product_groups_with_event_defaults_url 类型?或者可能与 request.env['HTTP_REFERER'] 有关,尽管这看起来不太可靠。

问题是,我仍然想要漂亮的网址,所以这些都不是:

http://localhost:3000/admin/commerce/product_groups/new?defaults%5Bname%5D=事件+名称+和+日期%3A+周一+一月+25+21%3A03%3A05+-0700+2010

What's the standard for applying default values to Rails models from links? Here's the situation:

There is a model in Spree called ProductGroup, which is a collections of settings defining how to find a Product (based on a bunch of property values you set). I want to be able to use this structure for defining common ProductGroups:

  • Event Products
  • Subscribable Products
  • Tickets
  • Donations
    ...

So if I am on a "Create Event" page, and the user clicks "Create Tickets", I want this to take them to the ProductGroups page, so they can create a new ProductGroup for that event, say "My Cool Event Products".

I want to do this as easy as I can without having to override/extend controllers/models. The solution I am thinking about using is to have a :defaults hash passed from a link_to method in a view to the controller, like so:

<%= link_to "Create Event-related Products", new_admin_product_group_url(:defaults => {:name => "Event Name and Date: " + Time.now.to_s}) %>

With that I could then write a generic plugin basically that just applies any properties inside params[:defaults] in the controller to the model.

Is there something like that out there? Or is there a better/recommended way to do this, maybe with defining a custom product_groups_with_event_defaults_url type thing? Or maybe something to do with request.env['HTTP_REFERER'], though that doesn't seem very reliable.

The catch is, I still want pretty urls, so none of this:

http://localhost:3000/admin/commerce/product_groups/new?defaults%5Bname%5D=Event+Name+and+Date%3A+Mon+Jan+25+21%3A03%3A05+-0700+2010

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文