0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

エラーでspecが通らない「ArgumentError: '0' is not a valid カラム名」

Posted at

状況

:カラム名1→str型
:カラム名2→str型
:カラム名3→int型
:カラム名4→int型

newアクションやupdateアクションのspecで
ArgumentError: '0' is not a valid カラム名の
エラーが起きる

結論

0は有効ではありません、と言われています。
整数(int型)として渡すべき内容が、文字列(str型)として渡されているため
起こっているいるエラーになります。

そのため、渡す値を整数に変換してあげれば解決します。

解決方法

controller
def ●●●_create_params
  params.require(:staff).permit(
    :カラム名1, :カラム名2, :カラム名3, :カラム名4
  ).tap do |v|
    v[:カラム名3] = v[:カラム名3].to_i
    v[:カラム名4] = v[:カラム名4].to_i
  end
end

updateも同様になります。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?