Controllers

Transferring my website to C9 and deploying on Heroku…what are controllers? Do I need them for my basic deployment? I don’t want to mess with any Ruby code until I get it deployed. Could I get away with my website being without controllers for now? Add them in later?

You cannot deploy a working Rails application without any controllers or route (at least one).
The default Rails application in Rails 4.x includes a ‘secret-hidden’ controller that produces a “WELCOME” page if you do not write any code.

If you are just getting your feet wet with Heroku (and Rails) I suggest you get the deployment cycle well-in-hand by setting up either a bare Rails 4.x app or using the Rails Scaffolding to build a rudimentary application.

Parkin, tutorials I followed had me change my config.rb file to include a route welcome#index. Then I generated the controller. However I was unable to find WHERE to put more files to link. That’s my issue. I tried placing files everywhere but was unable to get a successful link.

The index.html.erb file I placed in the welcome controller folder was modifiable and worked though.

It sounds like you need to better understand the structure of Rails.
There is a folder (Views) for the HTML/ERB files and one (Controllers) for the controllers - as you found when you wrote the controller - and one for the models (models).
I recommend you get a local, working Rails application (use rails server) before trying to deploy anything.

It is understandable that you want to get the workflow figured out in advance of building something ‘real’ but if it doesn’t work locally you will frustrate yourself when it is on Heroku and you have less capability to troubleshoot it.

I’m using C9 as my development environment. It’s a classic setup.

Can you read through that? I’m following that.

Once I follow all those steps, you’re saying that all my HTML files will be in the views folder? Just throw them in?

No. Not exactly.
Actually, anything you put in the "public’ folder will be directly available and accessible. For static HTML files that will suffice. But, of course, if you are building a site with only static HTML you could simply use Github Pages. If you want to learn and master Rails you need to build a dynamic site.

When you generate a Resource (which should be matched to a Controller and usually a Model or two) there is a folder created in the ‘views’ folder to match its name. That is where all the Views for that resource belong.

I guess I just don’t understand what those terms mean. I am having trouble wrapping my head around how pages (dynamic or static) fit into Models and Controllers.

Take a look at this official primer/tutorial.

Helpful…would my entire website technically just be one application? Keep in mind, my sites fairly static at the moment except for server-side code (currently in PHP) which is a blog script system, user/password login script, and subscriber email list. How would I set up my controllers in this case?

That is correct. You are constructing a ‘Rails Application’ which is inherently dynamic.
However, if you plan to use this as an exercise to learn and grow your skills, it can begin with solely static content.
In that way you can gradually, progressively modify it as you learn more.

I have successfully applied Rails Model/View/Controllers to provide a site that DOES NOT implicitly interact with the user and/or a database. Just as you can with PHP, or PERL.

I’ll do static for now and then as I write my Ruby code to convert my PHP scripts to Ruby, I can simply add Controllers which then interact dynamically with databases/ruby crap and returns what I need? E.g. think about this in PHP for now (since I know very ltitle rails)

Let’s pretend this is PHP. I create my static site. Whoopidy doo. Then I decide to make an e-mail script. Would I just (in one of my static pages) point to a controller (?) and inside of the controller there’s a page with the script that takes care of everything (pulling from db, sending the e-mail) etc?

No. That’s not how Rails works.

Rails is not comparable to PHP in this way. It is a framework, so it is more comparable to one of the PHP MVC frameworks (such as CakePHP), not to the language itself.

With Rails your app receives a request.
The router determines which controller is responsible for handling this and forwards it accordingly.
The controller receives the request, fetches or saves data from/to a model (if necessary) and then invokes the corresponding view (making any data it has retrieved available).
The view is then rendered.

Everything within a Rails app runs within this framework.
You cannot make a static page and drop odd bits of Ruby into it (as you can with PHP).

Interesting simple experiment for you.
Note the URL of this topic.
Change it to something like
http://www.sitepoint.com/community/t/see-what-happens/104335

Yes I was on Google Hangouts and @ParkinT helped me understand this. I have a semi decent understanding now.

In my routes.rb file, I have a root “welcome#index” set. That looks at the welcome controller and finds a view that is the same…I can create a method in the controller (like “def index”) if I want…I have no models right now but I could incorporate them in.

Then if I want to link to pages (like “sitehere.com/about”) I simply have to set up a view for it.

That just about sums it up? Have I misunderstood anything?

Not quite. Recall that you need a proper ROUTE; one that Rails will recognize and be able to respond with a Controller/Action combination.
But you are on the right track. I look forward to spending time with you again, one-on-one, and help build up your experience and understanding. There are so many things we did not discuss due to the minimal time I was able to spend with you.
Perhaps it would be beneficial if we collaborated on a Rails project; this port of your site would be a fine choice, or we choose something else.
Also, take advantage of the opportunity to read other people’s code by browsing (and cloning) projects on Github.

Ah, so when I make an HTML link (or any sort of reference in Ruby code?) for “about”…then that will look in my routes for what to do, and if I did something like “welcome#about” then that would be fine? It’d be the same controller just a different view.

Correct. And recall that I showed you the ‘routing’ will accept some wildcard tokens. They can be defined as optional by wrapping them in parentheses.

This may provide some help when you have questions about Routing.

1 Like

Think I got it…Currently trying to set up my databases at the moment but a little hesitant. I did add some CSS to my assets CSS file (welcome.css.scss) just to see if it would work but after we got off the video call last night, I left it alone; I have work early in the morning so I decided to call it a night.

By the way. When you can always check in the Routing that Rails currently “thinks” is setup, from the command line, with rake routes. This is a useful command that you quickly come to depend upon.


In response to your earlier question about links that reference other pages on your site, Rails provides some auto-magic shortcuts you should use in order to keep your code from becoming brittle. For example, within a view, you can reference the route to create a user with new_user_path; which is what you will see from the rake routes command (as an example, of course. This assumes you have a ‘user’ resource defined).

I just saw that Pragmatic Studio is having a 25% discount (for this week, I think) on their Rails course(s); which I highly, HIGHLY recommend - if you are able/willing to spend the money. That could be a quick route to success with Rails!

This book is free and should help you get started. I believe it is up to date with rails 4 as well.