Topic: Alt CSS eg Print CSS and Large Text CSS via button

I'd like a button that will reload the current controller/action/view but with an alternative CSS (eg, Print Version and Large Text Version).

Has anyone done this in Rails and if so are you able to provide me with your code?

Thanks

Re: Alt CSS eg Print CSS and Large Text CSS via button

I haven't done exactly what you're talking about but I have done some work to get css files specified by controllers.  I think you'd be interested in checking out this tutorial.

I think the best solution to what you're trying to do is have your controller action (or the whole controller) specify a default css file unless certain conditions are met.  Kinda like the following:

First, this goes in your ./app/views/layouts/application.rhtml file:

<html>
<head>
<title>my page</title>
<%= @stylesheets.collect { |file| stylesheet_link_tag file }.join("\n") if @stylesheets %>
</head>
</html

.

Then, do your controllers like this

# set up all the controllers to be able to specify custom stylesheets (with ones named after the controller showing up as default)
# you could also put this code in just one controller if you wanted.
class ApplicationController < ActionController::Base

  attr_accessor :stylesheets
  before_filter {|c| c.stylesheets ||= [] }

end

# and here's one way of implementing what you want:
class LargeOrSmallFontController < ApplicationController
  before_filter {|c| c.stylesheets << c.params[:large_font] ? 'large.css' : 'small.css' }
end


.

The above would print a stylesheet link that uses 'small.css' unless you sent the parameter 'large_font' equal to something non-empty.  For various font sizes you could do the following:

class LargeOrSmallFontController < ApplicationController
  before_filter {|c| c.stylesheets << c.params[:style] ? params[:style] : 'normal' }
end

.

The above would default to 'normal.css' unless you specified the name of some other stylesheet, then it would use whatever you tell it.  Notice also that the '.css' extension is purely optional.

Now, in case you just need to change font size and you don't otherwise have to reload the page, you may just want to use a javascript link.  There's a good howto here: alistapart.com

Good luck and I hope to hear how it works out!

Re: Alt CSS eg Print CSS and Large Text CSS via button

I just realized I didn't include any of the link code.  Here are some example buttons that could reload the page with a different stylesheet.

  button_to "Large Font", :action => 'show', :style => 'large'
  button_to "Small Font", :action => 'show', :style => 'small'

Also, you might want to store the stylesheet selection in the session if you want this change to last beyond one page view.  Ask if you want some example code for that.

Re: Alt CSS eg Print CSS and Large Text CSS via button

i want to know more about this code please can you send me all instructions

so @stylesheets we used it where ? we don"t see here in application controller

thanks for your reply