2014-11-12 1 views
1

나는 실제로 params.require의 목적을 이해하지 못합니다 -보다 구체적으로, param이 빠졌을 때 예외를 발생시키는 용도는 무엇입니까?params.require의 사용 사례는 무엇입니까?

는 내가 어떤 PARAMS가 주어진 요청에 대한 존재 할 수 있습니다 알고 있지만 그 중 하나가 누락 된 경우 왜 500 서버 오류를 반환 할 것인가? 조건부 논리를 사용하여 원하는 매개 변수에 대해 params를 트래핑하지 않고 flash[:error]을 대신 반환하는 이유는 무엇입니까?

+0

아이디어는 당신이 사용하는 것이'params.require', 다음 오류 https://github.com/rails/strong_parameters#handling-of-unpermitted-keys을 처리 할 수있다 – juanpastas

+0

당신이 언급하는 옵션입니다 또한 유효하지만 처음에는 params.require가 더 짧은 코드를 생성 할 수 있습니다. 아마도 ... – juanpastas

답변

0

이 강한 매개 변수를 사용하여 레일에 의한, Strong Parameters를 참조하십시오. 필자는 대답 모든 화이트리스트 및 매개 변수의 블랙리스트는, 레일 반대를 수행 필요로하는 다른 플랫폼과는 달리

의 하단에 강한 매개 변수 섹션을 붙여. 모든 것이 블랙리스트에 올라 있으므로 대량 할당을 통해 허용하려는 매개 변수를 허용해야합니다. 객체를 저장하려고하고 특정 PARAM을 허용하지 않은 경우 따라서

왜, 당신은 "허가되지 않은 매개 변수를"볼 로그/콘솔.

허용하려는 허용 및 허용 목록 매개 변수를 통해 저장/업데이트 작업을 수행하는 것이 가장 좋습니다.

강한 매개 변수 강력한 매개 변수와

는, 액션 컨트롤러 매개 변수가 허용 된 때까지 활성 모델 질량 할당에 사용하는 것은 금지된다. 즉, 대량 업데이트를 허용 할 수있는 속성에 대해 의식적으로 선택해야하므로 노출되어서는 안되는 것을 우발적으로 노출시키지 않습니다. 필요없고 노력 400 잘못된 요청으로 끝내고 소정 레이즈/복구 흐름으로 흐르는 또한

은 파라미터로 표시 될 수있다.

class PeopleController < ActionController::Base 
    # This will raise an ActiveModel::ForbiddenAttributes exception 
    # because it's using mass assignment without an explicit permit 
    # step. 
    def create 
    Person.create(params[:person]) 
    end 

    # This will pass with flying colors as long as there's a person key 
    # in the parameters, otherwise it'll raise a 
    # ActionController::ParameterMissing exception, which will get 
    # caught by ActionController::Base and turned into that 400 Bad 
    # Request reply. 
    def update 
    person = current_account.people.find(params[:id]) 
    person.update!(person_params) 
    redirect_to person 
    end 

    private 
    # Using a private method to encapsulate the permissible parameters 
    # is just a good pattern since you'll be able to reuse the same 
    # permit list between create and update. Also, you can specialize 
    # this method with per-user checking of permissible attributes. 
    def person_params 
     params.require(:person).permit(:name, :age) 
    end 
end 
관련 문제