LoginSignup
1
2

More than 5 years have passed since last update.

render :json したときのTypeError: nil is not a symbol

Posted at

なぜか、以下のような形でrender :jsonしたときにTypeError: nil is not a symbol時の対処について。

respond_to do |format|
    format.html
    format.json {render :json => result}
end

tableを作成したときに複合主キーを設定していてかつ、
model側で、複合主キーの設定が抜けていた場合に発生する可能性がありそう?

composite_primary_keysというgemを利用して、
modelファイルに複合主キーの設定を追記してあげることで解消しました。

手順
1.Gemfileにgem 'composite_primary_keys'を追加しbundle install
2.modelファイルに以下のような記載と追加

   # 主キー設定
  self.primary_keys = :column_a, :column_b

ちなみに複合主キーをtable側に指定するMigrationファイルは以下のような感じです。

class CreateTables < ActiveRecord::Migration
  def change
    create_table :tables, id: false do |t|
      t.string :column_a
      t.string :column_b
    end
    add_index :tables, [:column_a, :column_b], unique: true, name: 'index_name'
  end
end

エラー自体は解消されたんですが、理屈がさっぱりです...

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2