Designing Web Sites with Ruby on Rails - ruby-on-rails

I'm just learning Ruby on Rails. I've read a few books, I've watched lots of Railscasts, I've looked at some examples.
However, when working on my first serious project with Rails, I've gotten hung up on how to properly implement some very basic web site features that most tutorials I've seen lack.
For instance: Navigation menus. Lots of tutorials on how to make a static one, but what about dynamic? If I want to make a navigation bar that's different across pages, how would I go about doing that?
I think that the best way to learn things like this is by seeing example code. Are there any good open sourced sites in RoR? Any example code that I can check out?
I guess my question really results to using MVC. The basic idea is really basic, and I understand that. But it seems that most applications have each part completely separate from each other... what if I want to, say, combine data from two models to display on the same page? To take the example I posed, if I have a NavModel and a PageModel, can my PageController access both models? I guess I'd assumed that a PageController can only access a PageModel, and none other. All examples I've seen seem to operate this way...is that a faulty assumption?
Or am I totally missing the point, and is that 'doing it wrong?'

Open Source Rails is a repository of sites implemented in rails.

Which books have you read? I think a better understanding of MVC and Rails would help you more in the long run than sample code, which you may be tempted to copy without understanding.
The problem you're describing isn't really different from finding and displaying any kind of data, which I'm sure you've seen covered in snippets on blogs, etc. (using partials and/or layouts is maybe the only difference).
If you haven't read them already, you should try Agile Web Development with Rails and The Rails Way.
There are also several books that take you through building a sample application that include commentary to make sure you understand what you're doing.

This might help: Dynamic navigation menu using Menuitem model

Your controller can access any of your models, so if PageController needs to access your NavModel that's fine. I think typically the Nav controller or helper would contain the methods necessary to prepare the navigation view, but without knowing the details of your project I can't say for sure - if you think it's part of the Page logic, then put it there.
You may also be interested in this "What goes where" question.

Heres a howto on highlighting the current menu item in the page you are on
http://snippets.dzone.com/posts/show/2016

Related

rails user notifications for several models

I'm still learning rails but want to be sure I'm heading in the right direction. For several of my models I want to let a list of users know that updates have been made when a new record is created for example. I'd like to tie this to an email and in-app notification(doesn't need to be AJAX), next page refresh is fine.
I've been reading up on observers some and I think that's what I want but they seem somewhat controversial based on the blogs I've read. Services like Pusher seem to be overkill for my needs.
Would this be a good solution for this scenario? Also, can anyone point me to some example code that I could emulate?
Thanks!
Check out Rails Cast if you are trying to learn. Best free code base I have seen. Also check out this book another great learning tool. Agile Web Development with Rails (2nd edition).There might be some newer ones out but I have not checked.

Best practice in Rails when editing scaffold made pages

I have a best practice question. I have two classes, company and category. They have a many-to-many relationship. When clicking a category I shall go to a page showing all companies with the chosen category. Pretty straight forward.
My question is:
Should I list all the companies on the companies/index.html.erb after filtering the companies in the controller?
or
Should I list all the companies on the categories/show.html.erb page?
or
Should I do a completely new page, since it doesn't really fit into any of the two above?
Do you generally make a new page when your goal doesn't fit the scaffold made pages or do you use them quite freely?
I am working/learning alone with rails, so there are a lot of best practice questions popping up all the time.
Cheers Carl
A scaffold is a starting point, so you should always consider what you need in your web application.
In you case, I think all examples are fine, but again, it's really up to you.
If you are just learning Rails, stick to as many conventions as possible. Scaffolding is one of the ways Rails can help you get things started when you don't know how all of the pieces work together.
But like Oscar said, ultimately you must decide what your application will need.
The Ruby on Rails platform comes with many principles such as DRY etc.
The main part is the MVC architecture, the thing scaffolding does is let you see how this MVC is used correctly by a Model, View and Controller generation.
When you learn how this works you will be able to answer your own question, I could write my thoughts down but it is key for a Rails developer to understand the MVC structure so my suggestion is to read:
http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/
have fun

What is the best conceptual approach, in Rails, to managing content areas in what is otherwise a web application?

(A while back I read this great post: http://aaronlongwell.com/2009/06/the-ruby-on-rails-cms-dilemma.html, discussing the "Rails CMS Dilemma". It describes conceptual approaches to managing content in websites vs web apps. I'm still a beginner with Rails, but had a bit of a PHP background, and I still have trouble wrapping my brain around this.
A lot of what I run into is customers who want a website that is not 100% website, and not 100% web app... That is, perhaps there are several pages of business-to-public facing content, but then there are application elements, and the whole overall look is supposed to be cohesive. This was always fairly simple in PHP, as you just kind of dropped your app code into the PHP "script", etc (though I know there are plenty of cons to this platform and approach).
So I am wondering, what is the best approach in Rails for doing this?
Say you have an application with user authentication and some sort of CRUD stuff going on, where users collaborate on projects or something. Well, what is the optimal approach for managing the text/images of the "How This Site Works" and "Our Company" pages, which people may also want to view? Is it just simply having a pages controller and several text fields, with an admin panel on the back end that lets you edit those fields? Or is it perhaps a common approach to start off with something like Refinery, and then build on top of it for the non-content-driven areas of a site?
Sorry if this is a dumb question. It's just that I've read Hartl's book and others, and they never address this practical low-level stuff for a beginner... Sure, I can build a Twitter feed now, but what Twitter's "About" page (http://twitter.com/about)? I can't just throw text into a view and give that to a client... They want a super easy way to see the site tree, edit content areas, AND administrate/run their Twitter feed or whatever.
Thanks for your help.
I think you're looking for a CMS that runs as a plugin in your Rails application. If that's the case, I'd suggest that you try http://github.com/twg/comfortable-mexican-sofa

Does a "vertical" framework for RoR make sense?

I have been spending some time creating what I called a framework.
It was aimed at the creation of quiz likes games. It was supposed to have two players syncronized and present them a question to solve, etc.
I would like it to be fully customizable so I tried to develop components that can be put in or out of the pages. In the end the elements became slim ruby methods to add a whole bunch of Javascript and CSS to the pages.
Still the Javascript needs to connect to Ruby so methods supporting it are created but they will only be present when the component is present. Some components depend on one another making everything overly complex.
And after this attempt I wonder, is there is not a better and easier way to make a framework aimed to one kind of application on RoR? Or is the concept flawed or RoR in some way flawed?
Ruby on Rails is a framework on its own accord and is "opinionated software". This means that the writers of Rails looked at what would make most sense for creating a web application to them. Many people support the original developers views and so use Rails for their projects as well.
I think your concept of creating a quiz is a good one, but first you need to understand the rails stack. Depending on what you need exactly, you can create either an engine, plugin or whatever.
What I have seen a lot is that you specify what you need in your controller. (How you do that is up to you). All that information is stored in a class variable and transferred to the view where you can render everything you need with some helpers. The hard part is making it all generic enough to be reusable.
But, maybe Rails isn't the right tool for you. Maybe you need something more lightweight like Merb or even Sinatra.
There is no 'flaw' in Rails. Rails is not the 10**1000-in-one tool Java is. It's a framework that tries to do one way very good in a particular way. I think Rails can be the right tool for you, but you need to be skilled enough to wield the tool :)

Need help setting up scaffolding for this situation

I asked a question earlier, and got an excellent response, but, being a newbie to Rails and still getting the basics down, I need someone to show me how to set up some scaffolding for the situation that Hates_ was nice enough to outline for me. I've set up my application and whatnot, but I basically want to have, for example purposes:
A "Stories" table, for a list of stories
A "Pages" table, for a list of pages that can be attached to stories
A "LinkedPages" table, to link one page to multiple other pages
The idea is that users can create Stories. Stories have Pages. Pages link to other Pages in a branching sort of way, and Hates_ suggested using a LinkedPages table to assist the process of determining which pages link to which other pages.
If someone could use the example models Hates_ provided on the link above (or give me better ones if needed), and just show me how to get this started, I'd be extremely grateful. Rails has had the highest initial learning curve of any programming or scripting language I've learned in the past, I'm just starting to understand many of the basics.
Thanks in advance!
Here is an updated fast paced video that quickly presents some of the wicked sweet features in Rails via a somewhat contrived Blog tutorial.
http://media.rubyonrails.org/video/rails_blog_2.mov
The presenter crams a lot of information into a very short time frame and does paste in some prepared code from time to time, but over all he hits on many of the key features.
He explains how to set up scaffolding as well in about probably the last 5 mins or so.

Resources