Welcome/home page in Ruby on Rails - best practice

后端 未结 7 1503
温柔的废话
温柔的废话 2020-12-07 09:05

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

7条回答
  •  佛祖请我去吃肉
    2020-12-07 09:20

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

    Not only that, I'd seriously consider creating a new Model, not ActiveRecord-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.

    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.

提交回复
热议问题