简体   繁体   English

如何遍历对象参数哈希中的哈希?

[英]How do I iterate through a hash in an objects param hash?

Rails (and coding) rookie here (I'm sure I'm just missing basic syntax structural stuff), I've created a form where the user can add as many field pairs as they like via AJAX (barely). 这里的Rails(和编码)菜鸟(我确定我只是缺少基本的语法结构方面的东西),我创建了一个表单,用户可以通过AJAX(很少)添加任意多的字段对。 This form will collect column titles for a 'sheet' and the associated data type (int, str... etc). 该表格将收集“工作表”的列标题以及相关的数据类型(int,str ...等)。 The sheet will have item entries added later by users. 该工作表将具有用户稍后添加的项目条目。 I'm trying to make a Sheets controller create method that not only saves the title and description of the sheet, but also adds a record to the 'columns' table with the column title, data type and associated sheet id. 我正在尝试创建一个Sheets控制器的create方法,该方法不仅可以保存工作表的标题和说明,还可以向列的表中添加列标题,数据类型和相关工作表ID的记录。 When I submit the sheet form, I get the following params in the server terminal: (sorry I'm not sure how to wrap the code snippet) 提交工作表表单时,在服务器终端中得到以下参数:(对不起,我不确定如何包装代码片段)

{"utf8"=>"✓", "authenticity_token"=>"yMlnfO1EWptkEXp5+9AGCO5C3vHt62EUHoKjdWoUB8I=", "sheet"=>{"title"=>"test 33", "description"=>"Descriptions"}, "column"=>[{"title"=>"1", "type"=>"num"}, {"title"=>"2", "type"=>"int"}, {"title"=>"3", "type"=>"real"}, {"title"=>"fo", "type"=>"no"}], "commit"=>"Save Specsheet!"}

I'm trying to loop through the column hash to create a record on the columns table. 我正在尝试遍历列哈希,以在列表上创建一条记录。 Each hash would use the title and type values as entries on the table. 每个哈希将标题和类型值用作表上的条目。

My create method: 我的创建方法:

   def create
@sheet = Sheet.new(sheet_params)
@sheet[:column].each do |key, value|
  @column = Column.new
  @column[:column_title] = key
  @column[:column_data_type] = value
  @column.save
end

if @sheet.save
  redirect_to @sheet
else
  flash[:error] = "Error saving sheet."
  render :new
end

end 结束

My error is usually something like this: undefined method `each' for nil:NilClass 我的错误通常是这样的:nil:NilClass的未定义方法“ each”

**@sheet[:column].each do |key, value|**
  @column = Column.new
  @column[:column_title] = key
  @column[:column_data_type] = value

So I know I'm messing up referencing the column hash and its key and values. 所以我知道我搞砸了引用列哈希及其键和值。 I'm thinking I can .reduce something here? 我在想我可以在这里减少一些东西吗? I have no idea. 我不知道。 These kinds of basic structural questions don't really show up with googling, so please let me know what I'm doing wrong, and thank you for reading all of this! 这些类型的基本结构性问题实际上并不会出现在Google搜索中,因此请让我知道我在做什么错,并感谢您阅读所有这些内容! Cheers! 干杯!

WORKING CODE (sorry for weird formatting) 工作代码(对不起,格式怪异)

  def create
@sheet = Sheet.new(sheet_params)

column_params.each do |value|
  @sheet.columns.build(value.permit(:title, :data_type))
end

if @sheet.save
  redirect_to @sheet
else
  flash[:error] = "Error saving sheet."
  render :new
end

end 结束

      private
  def sheet_params
    params.require(:sheet).permit(:title, :description, :created_at, :updated_at, :column)
  end

  def column_params
    params.require(:column)
  end

When you are calling @sheet[:column] , you are referencing an instance of Sheet instead of the params that you are trying to cycle through. 当您调用@sheet[:column] ,您引用的是Sheet的实例,而不是您尝试循环浏览的参数。

If you are trying to associate Columns to Sheets in a has_many relationship, you can create Columns like: 如果您尝试将has_many关系中的Columns与Sheets关联,则可以创建Columns,例如:

column.each do |key, value| 
  @sheet.columns.new( 
    column_title: key
   column_data_type: value
  end
end  

(and then save block) in your controller. (然后保存块)在您的控制器中。 Where column are the params. 其中column是参数。 This will indicate that the column belongs to the sheet instance. 这将指示该列属于图纸实例。

If you are trying to make the Column record without the associations, you can do 如果您尝试在没有关联的情况下创建“列”记录,则可以执行

column.each do |key, value|
  Column.new( 
    column_title: key
    column_data_type: value
  end 
end

(and then save block) (然后保存块)

(Both assuming that your fields are named column_title and not just title .) (均假设您的字段被命名为column_title ,而不仅仅是title 。)

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

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