Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 创建新模型与在Rails中使用布尔条件_Ruby On Rails_Rails Models_Conditional - Fatal编程技术网

Ruby on rails 创建新模型与在Rails中使用布尔条件

Ruby on rails 创建新模型与在Rails中使用布尔条件,ruby-on-rails,rails-models,conditional,Ruby On Rails,Rails Models,Conditional,我正在尝试创建一个rails应用程序,它基本上是一个todo应用程序,允许用户在开始时从两种模式中选择一种。该模式将影响用户将看到的html输出,但基本后端结构将是相同的(列表、任务等)。例如,浅色模式使用浅色方案,深色模式使用深色方案。最好为Mode创建一个新模型,或者将Mode设置为布尔值(对于mode1为true,对于mode2为false),并使用不同的视图,无论Mode是true还是false 第一步 而不是布尔属性。我将创建一个名为scheme的字符串属性。最初只有两个条目,光明和黑

我正在尝试创建一个rails应用程序,它基本上是一个todo应用程序,允许用户在开始时从两种模式中选择一种。该模式将影响用户将看到的html输出,但基本后端结构将是相同的(列表、任务等)。例如,浅色模式使用浅色方案,深色模式使用深色方案。最好为Mode创建一个新模型,或者将Mode设置为布尔值(对于mode1为true,对于mode2为false),并使用不同的视图,无论Mode是true还是false

第一步 而不是布尔属性。我将创建一个名为scheme的字符串属性。最初只有两个条目,光明和黑暗,但随着时间的推移,这将允许您更轻松地添加其他方案。我将直接将其添加到用户,而不是添加模型和关联。任何一种方法都可能需要向模型中添加一个字段,也可能需要立即添加您正在使用的字段。这降低了复杂性,使您能够更有效地分配和检索属性,并允许更轻松的测试。您的模型可能如下所示:

class User < ActiveRecord::Base

  validates :scheme,
            presence: true,
            inclusion: {
              in: proc { User.schemes },
              allow_blank: true }

  class << self

    def schemes
      [ 'light', 'dark' ]
    end

  end

end

然后在
/app/views/users/index.html.erb
中,只需调用渲染特定部分,如
渲染“#{current_user.theme}_index”
或其他内容

不过,如果可能的话,我会非常认真地尝试使用css来完成这一切,然后再开始这一切

/app/views/users/index.html.erb
/app/views/users/dark_index.html.erb
/app/views/users/light_index.html.erb
/app/views/users/index.html.erb
/app/views/users/dark/_index.html.erb
/app/views/users/light/_index.html.erb