如何最好地在rails应用程序中组织资产?

时间:2022-09-11 20:40:09

As I continue to make progress on my app my stylesheets folder has quickly become overrun. I've read the rails guide on the asset pipeline, and scoured the internet for a reputable source and am still at a loss on the "rails way" to keep everything tidy.

当我继续在我的应用程序上取得进展时,我的样式表文件夹很快就会溢出。我已经阅读了关于资产管道的rails指南,并在互联网上搜索了一个有信誉的源代码,但仍然在“rails方式”上迷失,无法保持一切整洁。

Before I go and try and develop a system by myself I was hoping to get some suggestions from you guys.

在我尝试自己开发一个系统之前,我希望能从你们那里得到一些建议。

Is there a well known "best practice" when it comes to asset organization? If not, what has worked well for you?

当涉及到资产组织时,是否有一个众所周知的“最佳实践”?如果没有,什么对你来说是有效的?

As of now my setup is this:

现在我的设置是这样的:

app/assets/stylesheets >

application.css, application-RESPONSIVE.css

应用程序。css,application-RESPONSIVE.css

any code specific to the body, containers, navbar, footer

任何特定于主体、容器、导航条、页脚的代码

controller.scss, controller-RESPONSIVE

控制器。scss,controller-RESPONSIVE

*any code specific to that controller

*特定于该控制器的任何代码

lib/assets/stylesheets >

reset.css

reset.css

in application.css i use

在应用程序中。我使用css

*= require reset
*= require_tree .
*= require_self

To get all the styles however I've also been wondering if its better to set self before tree as a means of overriding the main styles if need be?

为了获得所有的样式,但是我也一直在想,如果需要的话,是否最好在树之前设置self作为覆盖主要样式的方法?

1 个解决方案

#1


3  

Is there a well known "best practice" when it comes to asset organization?

当涉及到资产组织时,是否有一个众所周知的“最佳实践”?

Organization of assets:

In Rails, pipeline assets can be placed inside an application in one of three locations: app/assets, lib/assets or vendor/assets.

在Rails中,管道资产可以放在应用程序中的三个位置之一:应用程序/资产、lib/资产或供应商/资产。

1) app/assets is for assets that are owned by the application, such as custom images, JavaScript files or stylesheets.

1) app/asset是应用程序拥有的资产,如自定义图像、JavaScript文件或样式表。

2) lib/assets is for your own libraries' code that doesn't really fit into the scope of the application or those libraries which are shared across applications.

2)lib/assets是属于您自己的库的代码,这些代码并不真正适合应用程序的范围或那些在应用程序*享的库。

3) vendor/assets is for assets that are owned by outside entities, such as code for JavaScript plugins and CSS frameworks.

3)供应商/资产是指外部实体拥有的资产,例如JavaScript插件和CSS框架的代码。

Is it better to set self before tree as a means of overriding the main styles if need be?

如果需要的话,把self放在树前作为重写主要样式的方法更好吗?

application.css is what is called a manifest file. Now, it wouldn't particularly make any difference by switching self with tree, but it would change it's position in the loading sequence. Consider this...

应用程序。css被称为manifest文件。现在,用树交换self不会有什么不同,但它会改变加载序列中的位置。考虑这个……

Once you’ve placed your assets in their logical locations, you can use manifest files to tell Rails (via the Sprockets gem) how to combine them to form single files. These manifest files contain directives - instructions that tell Sprockets which files to require in order to build a single CSS or JavaScript file.

一旦您将您的资产放在它们的逻辑位置,您就可以使用manifest文件告诉Rails(通过spro佝偻gem)如何将它们组合成单个文件。这些清单文件包含指令——指令告诉spro佝偻病需要哪些文件来构建一个CSS或JavaScript文件。

Additional info:

One other thing that Rails does, is generate stylesheets for you when you run the rails generate command in the terminal. On creation of a new controller, you'll see a stylesheet with the controller's name in your assets. It'll look something like app/assets/stylesheets/newcontroller.css.scss

Rails做的另一件事是,在终端运行Rails生成命令时为您生成样式表。在创建新控制器时,您将在您的资产中看到一个带有控制器名称的样式表。它看起来像app/assets/样式表/newcontroller.css.scss

Also, what steve klein said.

还有,steve klein说的。

Hope this helps you out.

希望这能帮到你。

UPDATE:

I've tried looking for examples on github but all of the apps I have found have been too simple and havent shown any advanced organization for a large amount of files.

我尝试过寻找github上的例子,但我发现的所有应用程序都过于简单,而且还没有为大量文件显示任何高级组织。

I encourage you to check out Open Source Rails website. I have looked at some projects there and they seem complex enough for you to be interested enough. Especially, look at the Discourse app. Discourse was co-founded by Jeff Atwood, co-founder of Stack Overflow. That I suppose speaks for itself ;)

我鼓励你去看看开源Rails的网站。我看过那里的一些项目,它们看起来很复杂,足以让你感兴趣。特别要注意的是,Discourse应用程序是由Stack Overflow的联合创始人Jeff Atwood创建的。我想这说明了一切;

#1


3  

Is there a well known "best practice" when it comes to asset organization?

当涉及到资产组织时,是否有一个众所周知的“最佳实践”?

Organization of assets:

In Rails, pipeline assets can be placed inside an application in one of three locations: app/assets, lib/assets or vendor/assets.

在Rails中,管道资产可以放在应用程序中的三个位置之一:应用程序/资产、lib/资产或供应商/资产。

1) app/assets is for assets that are owned by the application, such as custom images, JavaScript files or stylesheets.

1) app/asset是应用程序拥有的资产,如自定义图像、JavaScript文件或样式表。

2) lib/assets is for your own libraries' code that doesn't really fit into the scope of the application or those libraries which are shared across applications.

2)lib/assets是属于您自己的库的代码,这些代码并不真正适合应用程序的范围或那些在应用程序*享的库。

3) vendor/assets is for assets that are owned by outside entities, such as code for JavaScript plugins and CSS frameworks.

3)供应商/资产是指外部实体拥有的资产,例如JavaScript插件和CSS框架的代码。

Is it better to set self before tree as a means of overriding the main styles if need be?

如果需要的话,把self放在树前作为重写主要样式的方法更好吗?

application.css is what is called a manifest file. Now, it wouldn't particularly make any difference by switching self with tree, but it would change it's position in the loading sequence. Consider this...

应用程序。css被称为manifest文件。现在,用树交换self不会有什么不同,但它会改变加载序列中的位置。考虑这个……

Once you’ve placed your assets in their logical locations, you can use manifest files to tell Rails (via the Sprockets gem) how to combine them to form single files. These manifest files contain directives - instructions that tell Sprockets which files to require in order to build a single CSS or JavaScript file.

一旦您将您的资产放在它们的逻辑位置,您就可以使用manifest文件告诉Rails(通过spro佝偻gem)如何将它们组合成单个文件。这些清单文件包含指令——指令告诉spro佝偻病需要哪些文件来构建一个CSS或JavaScript文件。

Additional info:

One other thing that Rails does, is generate stylesheets for you when you run the rails generate command in the terminal. On creation of a new controller, you'll see a stylesheet with the controller's name in your assets. It'll look something like app/assets/stylesheets/newcontroller.css.scss

Rails做的另一件事是,在终端运行Rails生成命令时为您生成样式表。在创建新控制器时,您将在您的资产中看到一个带有控制器名称的样式表。它看起来像app/assets/样式表/newcontroller.css.scss

Also, what steve klein said.

还有,steve klein说的。

Hope this helps you out.

希望这能帮到你。

UPDATE:

I've tried looking for examples on github but all of the apps I have found have been too simple and havent shown any advanced organization for a large amount of files.

我尝试过寻找github上的例子,但我发现的所有应用程序都过于简单,而且还没有为大量文件显示任何高级组织。

I encourage you to check out Open Source Rails website. I have looked at some projects there and they seem complex enough for you to be interested enough. Especially, look at the Discourse app. Discourse was co-founded by Jeff Atwood, co-founder of Stack Overflow. That I suppose speaks for itself ;)

我鼓励你去看看开源Rails的网站。我看过那里的一些项目,它们看起来很复杂,足以让你感兴趣。特别要注意的是,Discourse应用程序是由Stack Overflow的联合创始人Jeff Atwood创建的。我想这说明了一切;