用户在 Rails 中选择 CSS 样式表
我正在 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
有没有办法制作类似的东西这个工作?现在它说 items/set_light 模板丢失,但我不想制作新模板,我只想调用 set_light 来更改样式表并刷新当前页面。关于如何实现这一目标的任何想法,或者可能是更好的方法来实现它?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了避免 Rails 查找“set_light”模板,您可以在已有模板的操作末尾添加渲染调用。例如,只需添加
或
其中 style_chooser 是您向用户呈现的视图或更改其样式
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
or
where style_chooser is the view you are presenting the user with or changing the style for