简体   繁体   English

用户在Rails中选择了CSS样式表

[英]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. 我正在Rails的网站上工作,希望用户能够将CSS样式表更改为浅色或深色主题。

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. 现在它说缺少projects / set_light模板,但是我不想创建新模板,我只想调用set_light来更改样式表并刷新当前页面。 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. 为避免Rails寻找“ set_light”模板,您可以在操作结束时为已有的模板添加一个render调用。 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 其中style_chooser是您要向用户展示或更改其样式的视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM