Ruby on Rails 中的欢迎/主页 - 最佳实践

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/349743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:49:52  来源:igfitidea点击:

Welcome/home page in Ruby on Rails - best practice

ruby-on-railsruby

提问by Espen

My homepage (or welcome page) will consist of data from two models (lets call them authors and posts). I am new to rails and not sure what is the best way to accomplish this.

我的主页(或欢迎页面)将包含来自两个模型的数据(我们称它们为作者和帖子)。我是 Rails 的新手,不确定实现这一目标的最佳方法是什么。

Should I create a new controller called welcome which gathers data from the authors and posts and then display them in the welcome index view? Or should I have a welcome view under the post model which also gets data from authors? Or any other way to accomplish this?

我应该创建一个名为welcome 的新控制器,它从作者和帖子中收集数据,然后在欢迎索引视图中显示它们吗?或者我应该在 post 模型下有一个受欢迎的视图,它也从作者那里获取数据?或者任何其他方式来实现这一点?

I understand how to do all this technically but just unsure what is the best practice method using the rails framework.

我了解如何在技术上完成所有这些,但只是不确定使用 Rails 框架的最佳实践方法是什么。

采纳答案by Robert K

The question is, is your home page just a landing page or will it be a group of pages? If it's just a landing page, you don't expect your users to hang around there for long except to go elsewhere. If it's a group of pages, or similar to an existing group, you can add an action to the controller it's most like.

问题是,您的主页只是一个登陆页面还是一组页面?如果它只是一个登陆页面,你不会期望你的用户在那里停留很长时间,除非去其他地方。如果它是一组页面,或者类似于现有组,您可以向它最像的控制器添加一个动作。

What I've done for my current project is make a controller named Static, because I need 3 static pages. The home page is one of these, because there isn't anything to see or do except go elsewhere.

我为当前项目所做的是创建一个名为 的控制器Static,因为我需要 3 个静态页面。主页就是其中之一,因为除了去别处之外没有什么可看或可做的。

To map a default route, use the following in routes.rb:

要映射默认路由,请在以下内容中使用routes.rb

# Place at the end of the routing!
map.root :controller => 'MyController', :action => :index

In my case this would be:

在我的情况下,这将是:

map.root :controller => 'static', :action => :index

If you wish to, you could create a controller just for this home page. I'd call it main, or something that you can remember which relates to the home page. From there you can get your data and your models and defer to the output view.

如果您愿意,您可以为此主页创建一个控制器。我将其称为主要的,或者您可以记住的与主页相关的内容。从那里你可以获取你的数据和你的模型,并遵循输出视图。

class MainController < ApplicationController
  def index
    @posts = Posts.find(:all, :limit => 10, :order => 'date_posted', :include => :user)
  end
end

Assuming you have your model relationships defined correctly, the template to match it will be very simple.

假设您正确定义了模型关系,匹配它的模板将非常简单。

Good luck, hope this helps.

祝你好运,希望这会有帮助。

回答by user664833

There doesn't seem to be a single best practice.

似乎没有一个最佳实践。

(1)The standard config/routes.rbfile seems to suggest that the root page (or home/welcome page) should be handled by welcome#index. If you were to be guided by that, then to generate the corresponding welcome#indexcontroller/action, you can use the following command:

(1)标准config/routes.rb文件似乎建议根页面(或主页/欢迎页面)应该由welcome#index. 如果您被引导,那么要生成相应的welcome#index控制器/动作,您可以使用以下命令:

rails generate controller Welcome index

Then, in config/routes.rb, you can remove the GET route (get "welcome/index") automatically added by the generator, and place the root route root 'welcome#index'(or root :to => 'welcome#index'in Rails < 4) at the top of the file, because it will probably be your most popular route and should be matched first.

然后,在 中config/routes.rb,您可以删除get "welcome/index"生成器自动添加的 GET 路由(),并将根路由root 'welcome#index'(或root :to => 'welcome#index'在 Rails 中< 4)放在文件顶部,因为它可能是您最受欢迎的路由,应该首先匹配。

Also remember to delete public/index.htmlin Rails < 4.

还记得public/index.html在 Rails 中删除< 4

(2)The official Ruby on Rails routing guideuses PagesController. It actually suggests pages#main, though to me it makes more sense to go with pages#home(because "homepage" is the ubiquitous term/concept). Additionally, this controller can handle other page-orientedactions such as pages#about, pages#contact, pages#terms, pages#privacy, etc.

(2)官方Ruby on Rails的路径向导的用途PagesController。它实际上暗示pages#main,尽管对我来说它更有意义pages#home(因为“主页”是无处不在的术语/概念)。此外,该控制器可以处理其他面向页面的动作,如pages#aboutpages#contactpages#termspages#privacy,等。

(3)The Ruby on Rails Tutorial, goes with static_pages#homeand static_pages#help, etc., though I don't like the idea of denoting this controller with "static". These pages will still likely have some dynamic aspects to them, particularly the homepage!

(3)The Ruby on Rails Tutorialstatic_pages#homeandstatic_pages#help等,虽然我不喜欢用“静态”表示这个控制器的想法。这些页面仍然可能有一些动态方面,尤其是主页!

(4)Though it does not discuss how to handle a homepage, RailsCast #117 on Semi-Static Pagessuggests yet another set of approaches to show-only resources.

(4)虽然它没有讨论如何处理主页,但RailsCast #117 on Semi-Static Pages提出了另一套显示资源的方法。

I feel preference toward 1 and/or 2. With the "and" scenario, you could use welcome#index and pages#about, etc., whereas with the "or" scenario, you could use pages#home, pages#about, etc. If forced to choose, I would go with option 2 just because you end up with less code. And btw, 2 and 3 are pretty much the same, apart from the word "static".

我更喜欢 1 和/或 2。对于“and”场景,您可以使用welcome#index 和pages#about 等,而对于“or”场景,您可以使用pages#home、pages#about,等等。如果被迫选择,我会选择选项 2,因为你最终得到的代码更少。顺便说一句,除了“静态”这个词之外,2 和 3 几乎相同。

回答by John

I asked myself something like this when I first started Rails. Here's what you need to know:

当我第一次开始使用 Rails 时,我问过自己这样的问题。以下是您需要了解的内容:

  • Models are not necessarily directly related to controllers and views.
  • 模型不一定与控制器和视图直接相关。

That is, a particular controller/view combination can work with as many models as you need to generate that particular page.

也就是说,特定的控制器/视图组合可以与生成该特定页面所需的任意数量的模型一起使用。

The purpose of the controller is to prepare the dataset you need to display, irrespective of what models are used to store that data.

控制器的目的是准备您需要显示的数据集,而不管使用什么模型来存储该数据。

The purpose of the view is to then display that data in the most appropriate way.

视图的目的是然后以最合适的方式显示该数据。

In other words, controller/view combinations are never 'under' a particular model. They use models, but are not under them in any hierarchical relationship. In fact, they are peersto whatever models they use.

换句话说,控制器/视图组合永远不会“在”特定模型之下。他们使用模型,但不处于任何层次关系之下。事实上,它们与所使用的任何模型都是对等的

I think the confusion comes from the scaffold generator example found in AWDR and other introductory texts, like:

我认为混淆来自 AWDR 和其他介绍性文本中的脚手架生成器示例,例如:

ruby script/generate scaffold model controller

ruby 脚本/生成脚手架模型控制器

I know that this implied relationship between model and controller/views confused me for a bit. But there is no strict relationship, really. If there were, then it would be very difficult to do anything complicated with the MVC approach. And clearly, that is not the case.

我知道模型和控制器/视图之间的这种隐含关系让我有点困惑。但是没有严格的关系,真的。如果有,那么用 MVC 方法做任何复杂的事情都会非常困难。显然,情况并非如此。

Hope this helps.

希望这可以帮助。

-- John

- 约翰

回答by allesklar

The best practice would be your first suggestion. Create a 'welcome' controller and call the records from whichever models you want. Have a root route point to that controller. Very clean and proper.

最佳做法将是您的第一个建议。创建一个“欢迎”控制器并从您想要的任何模型调用记录。有一个指向该控制器的根路由点。非常干净和适当。

回答by irakli

Please note that in Rails3, correct way to handle this is to add following line to the end of the routes.rb file:

请注意,在 Rails3 中,正确的处理方法是在 routes.rb 文件的末尾添加以下行:

root :to => "welcome#index"

and delete public/index.html.erb.

并删除 public/index.html.erb。

Please also note that welcome#index corresponds to index action in a WelcomeController and the code from The Wicked Flea's answer would look like:

另请注意,welcome#index 对应于 WelcomeController 中的索引操作,来自 The Wicked Flea 的答案的代码如下所示:

class WelcomeController < ApplicationController
  def index
    @posts = Posts.find(:all, :limit => 10, :order => 'date_posted', :include => :user)
  end
end

回答by Naoise Golden

This answer is as of Rails 3.2.1.

这个答案是从 Rails 3.2.1 开始的。

First set up a Controller for pages, named for example static:

首先为页面设置一个Controller,命名例如static

$ rails generate controller static

In file app/controllers/static_controller.rb:

在文件中app/controllers/static_controller.rb

class StaticController < ApplicationController
    def index       
    end
end

Create the new View file app/views/index.html.erb

创建新的视图文件 app/views/index.html.erb

And finally configure your config/routes.rb:

最后配置你的config/routes.rb

MyApp::Application.routes.draw do
   match 'home', :to => "static#index"
   root :to => "static#index"
end

This will make both /homeand /go to whatever you put in the View file you just created.

这将使双方/home/去到任何你把你刚刚创建的视图文件。

回答by Mike Woodhouse

Create a new controller named as appropriately as you can. SummaryController? StartController? DailyFrontPageController? You'll have an idea.

创建一个尽可能适当命名的新控制器。摘要控制器?启动控制器?DailyFrontPageController?你会有一个想法。

Not only that, I'd seriously consider creating a new Model, notActiveRecord-based, that collects the information from your Author and Post models (or whatever their real names are) for presentation in your view. The alternative is to assemble the data in the controller, which will almost certainly be messy - it was every time I tried it, and I tried it a lot. A separate model seems to end up a lot tidier.

不仅如此,我会认真考虑创建一个新的模型,而不是基于 ActiveRecord 的模型,它从您的 Author 和 Post 模型(或任何他们的真实姓名)收集信息以在您的视图中展示。另一种方法是在控制器中组装数据,这几乎肯定会很混乱——我每次尝试都是这样,而且我尝试了很多。一个单独的模型似乎最终会更整洁。

If the processing is relatively straightforward, why not try building the data in the controller first, then wrap the output in a Struct, then replace the Struct with a real class and move the construction there, refactoring all the way. It shouldn't add too much to the total time (most of the code can be reused) and you'll get a good idea of what works best for you.

如果处理相对简单,为什么不先尝试在控制器中构建数据,然后将输出包装在 Struct 中,然后将 Struct 替换为真正的类并将构造移到那里,一路重构。它不应该给总时间增加太多(大部分代码可以重用),并且您会很好地了解什么最适合您。