Rails 中上下文相关模型默认值(来自 link_to)
从链接向 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'] 有关,尽管这看起来不太可靠。
问题是,我仍然想要漂亮的网址,所以这些都不是:
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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论