LoginSignup
4
4

More than 5 years have passed since last update.

StrongParameter で、全カラムを許可するとき

Last updated at Posted at 2016-03-15

Scaffold などでも、こんな感じで生成される。

private

def article_params
  params.require(:article).permit(:title, :content)
end

全カラム許可するとき。

def article_params
  params.require(:article).permit(Article.column_names.map{|c| c.to_sym})
end

カラムに関係しないものも許可したり、一部だけ除外したくなったら Array操作で。

# 許可
def article_params
  params.require(:article).permit(Article.column_names.map{|c| c.to_sym} + %i(hoge fuga))
end

# 除外
def article_params
  params.require(:article).permit(Article.column_names.map{|c| c.to_sym} - %i(title))
end

配列も許可したくなったら。

def article_params
  params.require(:article).permit(Article.column_names.map{|c| c.to_sym}, :cateogory_ids => [])
end

:warning: 許可するかは、よーく考えてくだしあ。

4
4
1

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