Ruby on Rails 中的欢迎/主页 - 最佳实践 [英] Welcome/home page in Ruby on Rails - best practice

查看:32
本文介绍了Ruby on Rails 中的欢迎/主页 - 最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我是否应该创建一个名为welcome 的新控制器,它从作者和帖子中收集数据,然后将它们显示在欢迎索引视图中?或者我应该在 post 模型下有一个受欢迎的视图,它也从作者那里获取数据?或者有什么其他方法可以做到这一点?

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?

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

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

推荐答案

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

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.

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

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.

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

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

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

就我而言,这将是:

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.

这篇关于Ruby on Rails 中的欢迎/主页 - 最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆