简体   繁体   English

Rails 5形成字段到自定义参数

[英]Rails 5 form field to custom params

I have a class Reservation in which I want to put a collection (HABTM) of Resources, through an intermidiate class of ReservedResources. 我有一个类预留,我想在其中放置一个资源集合(HABTM),通过一个中间类的ReservedResources。 This is not a classic HABTM as in ReservedResources I want to store the quantity of the resource reserved. 这不是经典的HABTM,因为在ReservedResources中我想存储保留的资源数量。

On the form I want to display the available resources dynamically, something like this: 在表单上我想动态显示可用资源,如下所示:

    <% @resources.each do |r| %>
        <%= f.label "#{r.name}" %>
        <%= f.number_field "reserved_resources#{r.id}".intern %><br>
    <% end %>

The problem is that there is no method named "reserved_resourcesXX", where XX is the resource ID. 问题是没有名为“reserved_resourcesXX”的方法,其中XX是资源ID。 I want to store the quantity of the resource per its ID on the params hash, but I can't manage to do that... 我想在params散列上存储每个ID的资源数量,但我无法做到这一点......

Is it possible? 可能吗? If so, how? 如果是这样,怎么样? I want to avoid JS for this... 我想为此避免JS ......

I'll just start off with an example which I guess will get you going. 我将从一个例子开始,我想会让你去。

Scenario 脚本

Imagine a store selling products. 想象一下销售产品的商店。 Users can reserve products to later buy them. 用户可以保留产品以便以后购买。 They can also define how many of each product they want to reserve. 他们还可以定义他们想要保留的每种产品的数量。

class User
  has_many :reservations
  has_many :products, through: :reservations
end

class Product
  has_many :reservations
  has_many :users, through: :reservations
end

class Reservation
  belongs_to :user
  belongs_to :product
end

Now when creating your reservations model you can simply add an extra field quantity , resulting in this reservations schema: 现在,在创建预订模型时,您只需添加额外的字段quantity ,即可生成此预留模式:

create_table "reservations" do |t|
  t.integer  "user_id",
  t.integer  "product_id",
  t.integer  "quantity",
  t.datetime "created_at"
  t.datetime "updated_at"
end

So you take a basic HABTM relationship and add the extra field. 因此,您采用基本的HABTM关系并添加额外的字段。 Thats exactly what HABTM is for, to add extra info when it is required. 这正是HABTM的用途,可在需要时添加额外信息。

Your usecase 你的用例

Adapt this to your models and I assume the html forms will be kinda straightforward from there on. 适应你的模型,我认为从那里开始,html表单将是直截了当的。

Actually what you should have is something like: 实际上你应该拥有的是:

class Reservation < ApplicationRecord
  has_many :reserved_resources
  has_many :resources, through: :reserved_resources
end

class Resource < ApplicationRecord
  has_many :reserved_resources
  has_many :reservations, through: :reserved_resources
end

class ReservedResource < ApplicationRecord
  belongs_to :reservation
  belongs_to :resource
end

Now assuming you have reserved_quantity attribute in reserved_resources table. 现在假设您在reserved_resources表中有reserved_quantity属性。

Example of use from rails console: 从rails控制台使用的示例:

resource = Resource.create(name: 'resource 1')
reservation = Reservation.create(name: 'reservation 1')
resource.reserved_resources.create(reservation: reservation, resource_quantity: 20)

reservation = Reservation.create(name: 'reservation 2')
resource.reserved_resources.create(reservation: reservation, resource_quantity: 10)

Sanity check: 完整性检查:

resource.reservations.count
=> 2

you can then get total quantity of reserved resources for a particular resource by doing: 然后,您可以通过执行以下操作获取特定资源的预留资源总数:

resource.reserved_resources.sum(:resource_quantity)
 => 30

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM