用户在Rails中选择的CSS样式表 [英] User selected CSS stylesheet in Rails
问题描述
我正在Rails的网站上工作,我希望用户能够将CSS样式表更改为浅色或深色主题。
我在我的视图中有这个,所以我可以为样式表使用一个变量:
<%= stylesheet_link_tag @current_stylesheet%>
我试图通过在我的视图中有一个链接来改变这个变量:
<%= link_to'Light Theme',:action => set_light,:id => @projects%>
在我的控制器中调用此函数:
class ProjectsController< ApplicationController
def set_light
@current_stylesheet ='light'
end
end
b $ b
有没有办法让这样的工作?现在它说,项目/ set_light模板缺失,但我不想创建新模板,我只想调用set_light来更改样式表和刷新当前页面。
以避免有轨道寻找一个' set_light模板,您可以向已经拥有的模板添加对操作结束的render调用。例如,只需添加
render:action => 'style_chooser'
或
render:template => 'style_chooser'
其中style_chooser是向用户呈现视图或更改
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 templates, 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
这篇关于用户在Rails中选择的CSS样式表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!