2012-10-05 5 views
0

저장하기 전에 일부 필드를 확인하고 기본 브라우저 언어로 변경하고 싶습니다.before_filter에서 request.env를 사용하는 방법은 무엇입니까?

def update 
@website = Website.find(params[:id]) 
    if @website.language == "Automatic (by user's browser language)" 
    @website.language = full_language(request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first) 
end 
respond_to do |format| 
    if @website.update_attributes(params[:website]) 
    format.html { redirect_to @website, 
     notice: 'Note: code has been updated. Please replace the code you have on your website with the code below. Only then changes will take effect.'} 
    format.js 
    end 
    end 
end 

내가 확인해야합니다 :

if @website.language == "Automatic (by user's browser language)" 
    @website.language = full_language(request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first) 
end 

을 그리고 그것은 작동 조치를 만들 수 있지만 갱신에

나는 필터 before_save 사용하고 싶습니다.

어떻게하면됩니까?

+0

요청 개체는 모델에서 직접 액세스 할 수 없습니다. – Amar

+0

그래서 사용자가 무엇을 선택할지 제어 할 수 있습니까? 내 업데이트 작업을 게시 할 것입니다. 업데이트 작업에서 – skrypalyk

답변

0

request 개체는 모델에서 사용할 수 없습니다. 컨트롤러 레이어에서 이러한 조정을 수행해야합니다.

def action 
    if @model.language == "Automatic (by user's browser language)" 
     @model.language = full_language(request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first) 
    end 
    if @model.save 
     ... 
    else 
     ... 
    end 
end 

private 

def full_language 
    ... 
end 
+0

이 작동하지 않습니다 ... – skrypalyk

+0

내 코드를보세요. – skrypalyk

+0

컨트롤러에'full_language' 메소드를 넣었습니까? 내 업데이트 답변을 참조하십시오. 또한 데이터베이스의 "자동 (사용자 브라우저 언어에 의한)"과 같은 선택 상자 값을 저장하는 것은 끔찍한 일입니다. 언어 약어 또는 정수를 사용하는 것이 좋습니다. – jibiel

1

저장하기 전에 모델에서는 일반적으로 요청을 모델에서 사용할 수 없습니다.

그러나 실제로 수행하고 싶은 경우 확인 방법에 대한 자세한 내용은 http://m.onkey.org/how-to-access-session-cookies-params-request-in-model을 확인하십시오.

- 편집 -

몇 가지 방법이 있습니다. attr_accessor :request_language 다음, 모델 모델 컨트롤러에서 request.env['HTTP_ACCEPT_LANGUAGE']을 통과 :

내 마음을 건너 첫 번째는 추가 점이다

if @website.update_attributes(params[:website]) 
    @website.request_language = request.env['HTTP_ACCEPT_LANGUAGE'] 
    # ... 

을하고 몇 가지 수정을 전에했던 것처럼 지금 당신은 진행할 수 :

def auto_language 
    if self.language == "Automatic (by user's browser language)" 
     self.language = full_language(self.request_language.scan(/^[a-z]{2}/).first) 
    end 
end 

내가 생각할 수있는 두 번째 방법은 제어기에서 이전/이후 필터를 사용하여 모델에 전달되기 전에 매개 변수에 영향을주는 것입니다. 당신이 뭔가를 업데이트 할 경우

...

+0

Krule, 내 작업을 확인하고 더 나은 방법을 말해 주시겠습니까? – skrypalyk

+1

Checked, said :) – Krule

+0

나는 공포에 빠져 있었다. 컨트롤러에서 모든 필터를 만들었고 모두 작동했다.) – skrypalyk

1

후 조치를 만들 수 있지만 작업은 다음 방법 XYZ 위의 예

# write in your model 
after_create :xyz 
def xyz 
... 
... 
end 

에게 조치를 생성 한 후 전화를 참조 업데이트하지. 업데이트를 호출하면 호출되지 않습니다.

+0

after_update로 만들고 싶습니다. 업데이트 액션에서만 어떻게 만드나요? 컨트롤러에서. – skrypalyk

+0

둘 다 after_create & after_create로 만들고 싶다면 해당 메소드를 after_save : xyz로 호출하십시오. 하지만 그것을 after_update로 만들려면 after_update로 호출하십시오 : xyz –

+0

before_save로 호출 할 수도 있습니다 : xyz는 모두 –

0

해시의 :language 값을 변경해야합니다. 궁극적으로 @website.update_attributes으로 전달됩니다. 컨트롤러 코드를 더 읽기 쉽게 만들기 위해 조건부를 모델 레이어로 이동하는 것이 좋습니다.

# app/controllers/websites_controller.rb 
def update 
    @website = Website.find(params[:id]) 
    if @website.language_automatic? 
    params[:website][:language] = full_language(request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first) 
    end 
    respond_to do |format| 
    if @website.update_attributes(params[:website]) 
     format.html { redirect_to @website, 
     notice: 'Note: code has been updated. Please replace the code you have on your website with the code below. Only then changes will take effect.'} 
     format.js 
    end 
    end 
end 

# app/models/website.rb 
def language_automatic? 
    language == "Automatic (by user's browser language)" 
end 
+0

에서 작동합니다. 감사합니다. 이미 필터를 추가 한 후에는 더 쉽게 사용할 수 있습니다. – skrypalyk