Rails 4 Best in place 精选集 [英] Rails 4 Best in place select collection

查看:35
本文介绍了Rails 4 Best in place 精选集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法在任何地方找到我的问题的答案,所以如果有人可以帮助我,我正在努力更好地理解这一点.

I can not seem to find a answer to my question anywhere, so please help me if anyone can I am trying to understand this better.

我有一个方法可以将变量传递给另一个模型的方法,以从表中获取 packlist_id.例如在我的发票模型中:

I have a method that passes variables into another model's method to get packlist_id's from a table. such as in my invoices model i have:

def packlistid
  Shipperline.packlist(@customerid, @shipqty)
end

customerid 来自我的 Invoice 类中的另一个方法(效果很好),shipqty 也来自我所做的另一个方法查询.

The customerid is from another method in my Invoice class (which works amazing) and the shipqty is also from another method query that i did.

然后该方法进入 Shipperline 模型,该模型有一个方法 self.packlist(customerid, shipqty):

Then that method goes into Shipperline model which has a method self.packlist(customerid, shipqty):

def self.packlist(customerid, shipqty)
  Shipperline.joins(:customerorderline, :shipper).select("cust_order_id").where(cust_order_id:      customerid).pluck(:packlist_id).uniq
end

然后在我看来我有:

best_in_place invoice, :packlsitid, :as => :select, :class=>"best_in_place", :collection => invoice.packlistid

这很好,因为我得到了一个充满数字的下拉选择框,例如

This is great cause i get a drop do select box filled with numbers such as

76433

53422

43156

34231

但是,当我选择 76433 之类的值时,进入数据库的值为 1.我希望它是完全相同的值 76433.

However, when i select one such as the 76433 the value that goes into the database is a 1. Where i want it to be the exact same value 76433.

select 时的 53422 将值 2 放入数据库中,依此类推.

The 53422 when select puts a value of 2 in the database and so on.

这是 rails 4 的最佳位置版本 3.0.1

This is rails 4 with best in place version 3.0.1

如果有人可以帮助我,我将不胜感激.

If anyone can help me i would greatly appreciate it.

推荐答案

你可以在代码 为选择构建选项,它将始终生成一个带有从 1 开始的整数键的散列.这样,您必须将其映射回到服务器上的实际值,通过生成相同的集合并从所选索引中选择结果 - 1,因此在您的控制器中

You can see on the code that builds the options for the select that it will always generate a hash with integer keys that will start from 1. That way, you'd have to map it back to the actual value on the server, by generating the same collection and picking the result from the selected index - 1, so in your controller

@selected_packlistid = @invoice.packlistid[params[:packlsitid] - 1]

这可能有点麻烦,特别是如果 #packlistid 结果在此期间有可能发生变化.所以,我建议你依赖其他 一段帮助程序代码,它指出当传递的值集合不是数组时,它只保留集合.然后你需要做的是生成packlistid并将其转换为key和value相同的hash

Which can be a bit cumbersome, specially if there's a chance that #packlistid result has changed in the meantime. So, I'd recommend you to rely on this other piece of the helper code which states that when the collection of values passed is not an array, it just preserves the collection. Then what you need to do is to generate the packlistid and transform it in a hash where the keys and values are the same

list = @invoice.packlistid
@select_values = Hash[list.zip(list)]

然后将其传递给帮助程序

and then pass it on to the helper as

best_in_place invoice, :packlistid, collection: @select_values

作为 :collection 选项将被使用 此处.请注意,它期望作为第二个参数传递给帮助程序的属性返回应标记的值,如您所见 此处 调用方法时,此处 设置为显示值时.

as the :collection option will be used as seen here. Notice that it expects that the attribute passed on as the second argument to the helper to return the value that should be marked, as you can see here when the method is called, and here when it is setup as the display value.

这篇关于Rails 4 Best in place 精选集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆