简体   繁体   English

将数组作为参数传递-ruby

[英]passing array as a parameter - ruby

This is inside the region controller file 这在区域控制器文件中

def create
    postal_code_list = params[:region].delete(:postal_code_list)
    @region = Region.new(params[:region])
    authorize! :create, @region
    assign_postal_codes(postal_code_list)

This is inside the seed file 这在种子文件里面

region = Region.create({
     name: "region1",
     postal_code_list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
})

I am attempting to create a few default values for a web app in testing. 我正在尝试为测试中的Web应用程序创建一些默认值。 I am exceptionally new to ruby, so that is the reason for such a basic question. 我对红宝石异常陌生,所以这就是提出这样一个基本问题的原因。 When I run in debug mode, the page displays the name:region1, however the postal codes do not show up. 当我在调试模式下运行时,页面显示名称:region1,但是邮政编码不显示。 I'm unsure as to how to pass the postal_code_list array into the controller file. 我不确定如何将postal_code_list数组传递到控制器文件中。 What am I missing? 我想念什么?

If you want to create seed for regions with seed run this steps: 如果要为具有种子的区域创建种子,请运行以下步骤:

in seeds.rb 在seed.rb中

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].each do |rc| 
  Region.create(name: "region#{rc}", postal_code_list: rc}
end

And then in the command line: 然后在命令行中:

rake db:seed

This will create 10 regions named: "region1", "region2"... and postal_code_list: 1, 2... 这将创建10个名为:“ region1”,“ region2” ...和postal_code_list:1、2 ...的区域。

Your controller is doing nothign as Region.create is doing a query, not a request. 您的控制器执行不正常操作,因为Region.create执行的是查询而不是请求。

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

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