Rails 4에는 strong_parameters가 포함되어 있습니다.이 중 큰 추가 기능이 있지만 문제가 있습니다. 나는 다형성 모델 Comment
을 가지고 있으며, 필자가 필요로하는 매개 변수를 받아들이려면 컨트롤러를 사용할 수 없다.Rails 4의 다형성 모델에 ForbiddenAttributesError
경로 :
resources :articles do
resources :comments
end
모델 : 여기 (명확성을 위해 단축) 내 코드는
class Article < ActiveRecord::Base
has_many :comments, :as => :commentable
end
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
end
컨트롤러 : 기사에 양식에서
class CommentsController < ApplicationController
before_action :get_commentable
def create
@comment = @commentable.comments.new(comment_params)
if @comment.save
redirect_to @commentable, :notice => "Thank you!"
else
render :new
end
end
private
def get_commentable
resource, id = request.path.split("/")[1,2]
@commentable = resource.singularize.classify.constantize.find(id)
redirect_to :home unless defined?(@commentable)
end
def comment_params
params.require(:comment).permit(:title, :message)
end
end
게시 PARAMS (# 쇼) :
그것은 작동해야처럼3210{"authenticity_token"=>"v70nN8aFpofNw9vbVjhpsm9SwLOwKlOpNOEOTozUwCk=",
"comment"=>{"title"=>"Test","message"=>"Testing"},
"article_id"=>"1"}
나에게 보이는, 그러나 무엇이든 나는 ActiveModel::ForbiddenAttributesError in CommentsController#create
얻을 시도 - 내가 컨트롤러에
def comment_params
params.permit!
end
을 시도 할 경우에도. 나는 다른 (비 다형성) 모델에 그런 문제가 없기 때문에 그것이 다형성과 관련이 있다고 생각됩니다. 어떤 아이디어?
사실,이 내가 역할 기반 권한 부여를 할 사용하고 캉캉으로 인한 오류가 보인다을 - 이를 사용 불가능으로 설정하면 주석을 작성할 수 있습니다. 흠. –