Rails 3.2.13 と strong_parameters gem をいじっています。ActiveModel::ForbiddenAttributes
開発中のテスト中に例外が発生するかどうかを知りたいですか?
私の投稿モデルには:title
and:content
がありますが、許可から削除する:title
とエラーは発生しませんが、フラッシュ通知で編集ページにリダイレクトされるため、レコードが保存されます。とはいえ、それは当然のことながら を変更しませんでし:title
た。これはデフォルトの動作ですか?
def post_params
params.require(:post).permit(:content)
end
発生した例外を取得するために何か他のことをする必要があるかどうかを知りたかったのです。
Gemfile:
# Gemfile
gem 'rails', '3.2.13'
gem "strong_parameters"
アプリケーション構成:
# config/application.rb
config.active_record.whitelist_attributes = false
投稿モデル:
# post.rb model
class Post < ActiveRecord::Base
include ActiveModel::ForbiddenAttributesProtection
end
ポストコントローラー:
# post_controller.rb
class PostsController < ApplicationController
def update
@post = Post.find(params[:id])
if @post.update_attributes(post_params)
redirect_to edit_post_path(@post), flash { success: "Post updated" }
else
render "edit"
end
end
private
def post_params
params.require(:post).permit(:title, :content)
end
end