레일을 찾아보십시오.
-
20-12-2019 - |
문제
나는 모바일 FU 보석을 사용하여 일부 사용자 에이전트 탐지를 수행하므로, 클라이언트에 따라 .html 또는 .mobile 확장 템플릿을 제공 할 수 있습니다
이제이 부분은 정말로 잘 작동하지만, 나는 그보기 폴더가 두 배의 파일을 두 배로 묶어 버리지 않는다는 것을 좋아하지 않습니다.
app/views/profiles/show.mobile.haml
app/views/profiles/show.html.haml
app/views/profiles/edit.mobile.haml
app/views/profiles/edit.html.haml
등, etc
내가 무엇을하고 싶은지 대신 :
app/views/profiles/html/show.html.haml
app/views/profiles/html/edit.html.haml
및
app/views/profiles/mobile/show.mobile.haml
app/views/profiles/mobile/edit.mobile.haml
및 레일이 자동으로 요청에 따라 파일의 올바른 폴더 / 디렉토리를 찾습니다. 이는 할 수 있습니까?
어쩌면 이것이 정말 쉽게 할 수있는 일이며, 이것이 상자에서 나오는 행동인지 알려주세요 ..
고맙습니다
해결책
Rails 4.1에는 ActionPack 변형 (모바일 fu 보석처럼 사용자 에이전트를 감지합니다.
기본적으로 ApplicationController에서 예를 들어 추가 할 수 있습니다.
before_action :detect_device_format
private
def detect_device_format
case request.user_agent
when /iPad/i
request.variant = :tablet
when /iPhone/i
request.variant = :phone
when /Android/i && /mobile/i
request.variant = :phone
when /Android/i
request.variant = :tablet
when /Windows Phone/i
request.variant = :phone
end
end
.
프로필 스코틀러기가 있다고 가정 해 봅시다.이제이 작업을 수행 할 수 있습니다 :
class ProfilesController < ApplicationController
def index
respond_to do |format|
format.html # /app/views/profiles/index.html.erb
format.html.phone # /app/views/profiles/index.html+phone.erb
format.html.tablet # /app/views/profiles/index.html+tablet.erb
end
end
end
.
귀하의 질문에 : 다른 폴더 / 디렉토리에서 파일을 찾으려면 다음을 수행 할 수 있습니다.
format.html.phone { render 'mobile/index' } # /app/views/mobile/index.html+phone.erb
.
또한 좋은 자습서 그것을 사용하는 방법.