문제

I'm coding on a little web app using http://padrinorb.org (haml+lesscss) for both mobile (android/iPhone/iPad) and desktop users, and as such, I wish to display slightly different html/css for the different devices.

To determine the various clients I use rack-mobile-detect which works great so far.

To get the layout to become layouts/application.mobile i use these helpers/wrappers for render

def preferred_extension 
  if request.xhr?
    "js"
  elsif env["X_MOBILE_DEVICE"]
    "mobile"
  else
    "html"
  end 
end 

def preferred_layout 
  if preferred_extension.eql? "html"
    "application"
  else
    "application.#{preferred_extension}"
  end 
end 

def render_preferred filename
  filename = "#{filename}.#{preferred_extension}"
  if request.xhr?
    layout_file = false
  else
    layout_file = "layouts/#{preferred_layout}".to_sym
  end
  render filename, :layout => layout_file
end 

However, this doesn't help me when dealing with partials... and it feels like somebody must have a better solution for this than just wrapping render and partial in a helper.

What I want is to not sprinkle my controllers and views with weird code just to render the correct partial/file with the correct layout.

I would like the render discovery to find out which file to use, and falling back to filename.haml if there is no filename.#{preferred_extension}.haml.

In rails there is this mime type thing that I used in a previous project, but I haven't find anything similar for padrino (sinatra)

도움이 되었습니까?

해결책

I solved it by setting @_content_type to preferred_extension in a before block inside each app/controllers/

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top