嵌套表单没有抱怨,但没有保存数据

时间:2016-09-05 03:50:42

标签: ruby-on-rails ruby forms activerecord nested

创建我的民意调查的表单正确显示并且轮询本身已创建,但答案选项未保存...

我正在使用rails 5.0.0.1

这是我的db:

INNER JOIN

-

Sub RunSQL()    
    Dim conn As Object, rst As Object
    Dim strConnection As String, strSQL As String
    Dim i As Integer

    Set conn = CreateObject("ADODB.Connection")
    Set rst = CreateObject("ADODB.Recordset")

    ' CONNECTION STRINGS (DRIVER VERSION COMMENTED OUT)
'    strConnection = "DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};" _
'                      & "DBQ=C:\Path\To\Workbook.xlsm;"
    strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" _
                       & "Data Source='C:\Path\To\Workbook.xlsm';" _
                       & "Extended Properties=""Excel 12.0;HDR=YES;"";"

    strSQL = " SELECT t1.*, t2.[EmailAddress]" _
                & " FROM [MainList$] t1" _
                & " INNER JOIN [EmailList$] t2" _
                & " ON t1.FirstName = t2.FirstName" _
                & " AND t1.LastName = t2.LastName" _
                & " AND t1.HouseNumber = t2.HouseNumber" _
                & " AND t1.StreeAddress = t2.StreetAddress;"            
    ' OPEN CONNECTION
    conn.Open strConnection
    rst.Open strSQL, conn

    ' COLUMN HEADERS
    For i = 1 To rst.Fields.Count - 1
        Worksheets("Results").Cells(1, i) = rst.Fields(i).Name
    Next i        
    ' DATA ROWS
    Worksheets("Results").Range("A2").CopyFromRecordset rst

    rst.Close: conn.Close    
End Sub

-

我的模特:

class CreatePolls < ActiveRecord::Migration[5.0]
  def change
    create_table :polls do |t|
      t.string :question

      t.timestamps
    end
  end
end

-

class CreateAnswerOptions < ActiveRecord::Migration[5.0]
  def change
    create_table :answer_options do |t|
      t.references :poll, foreign_key: true
      t.string :text
      t.integer :nbvotes, :default => 0

      t.timestamps
    end
  end
end

我的控制员:

class AnswerOption < ApplicationRecord
  belongs_to :poll
end

和我的观点:

class Poll < ApplicationRecord
  has_many :answer_options, :dependent => :destroy
  accepts_nested_attributes_for :answer_options
end

我无法找到数据未保存的原因...我可以在rails控制台中创建它们,并在show动作中显示它们。我尝试过添加和删除&#39; s在poll或answer_option的命名中没有成功......

1 个答案:

答案 0 :(得分:1)

你能用这段代码吗?您需要允许answer_options_attributes参数,因为它是由嵌套属性自动创建的。

def poll_params
  params.require(:poll).permit(:question, answer_options_attributes: [:text])
end