render :json したときのTypeError: nil is not a symbol
2710 ワード
なぜか、以下のような形で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
エラー自体は解消されたんですが、理屈がさっぱりです...
Author And Source
この問題について(render :json したときのTypeError: nil is not a symbol), 我々は、より多くの情報をここで見つけました https://qiita.com/TakakiSato/items/8031e56ff77a22a61898著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .