User selected CSS stylesheet in Rails
I'm working on a site in Rails and I'd like for the user to be able to change the CSS stylesheet to either a light or dark theme.
I have this in my view so that I can use a variable for the stylesheet:
<%= stylesheet_link_tag @current_stylesheet %>
I tried to change that variable by having a link in my view something like this:
<%= link_to 'Light Theme', :action => "set_light", :id => @projects %>
that calls this function in my Controller:
class ProjectsController < ApplicationController
def set_light
@current_stylesheet = 'light'
end
end
Is there a way to make something like this work? Right now it says that the projects/set_light template is missing, but I don't want to make new templa开发者_Python百科tes, I'd just like to call set_light to change the stylesheet and refresh the current page. Any ideas on how to accomplish this, or maybe a better way to approach it?
to avoid having rails look for a 'set_light' template you can add a render call to the end of your action for a template you already have. for instance, just add
render :action => 'style_chooser'
or
render :template => 'style_chooser'
where style_chooser is the view you are presenting the user with or changing the style for
精彩评论