Ruby on Rails simple website layout - ruby-on-rails

I'm learning RoR, I've read some tutorials (railstutorial for the first one),
but I've a problem to define the logic layout of the my first simple website.
The structure is:
When you go to mysite.com you see a welcome page with the signup form or the link for login.
If you signup or you login into the site, you are at mysite.com/dashboard and you see a list of your messages.
You can go to mysite.com/$username and you see a page with a form where you can write a message for the $username.
Stop. That's it. It's very simple, I know, but is for learning.
The problem is this: I'm new to MVC paradigm and I don't know how structure the logic layout of my app. Of course there'll two models: User and Message. But for controllers? And which functions in any controllers? Should I use scaffolding?
Please give me a help, I'm very confused.
Thank you.

Controllers are the logic for the data, so to login/sign-up is really validating/creating a user, if you need to view the users dash board, well that's a look up on the user data so he goes there as well.
The messages, that will be a separate controller that can create/view messages!

As others have pointed out, your controllers contain the logic for your code and invoke views based on that logic by rendering or redirecting to pages. You can define whatever actions you want in your controllers, and then use routes to map a particular URL to a controller action. That being said, Rails gets a lot easier if you "go with the flow" and make some simple assumptions about the actions that could happen. Both your users and your messages represent rows in their respective database tables. There's no much you can do to a row in a database table - you can Create it, Read it, Update it, or Delete it (CRUD). If you define your actions in terms of these four logical actions, Rails lets you generate some easy routes.
You can back into any URL schema that you want, but what you are describing is:
Read the messages that are for a user on the dashboard
Create a message for a user when you go to another page (mysite/username)
Each of these maps to a CRUD action that you should be defining in your controllers.
Agreed also with other advice to simply do a few more tutorials that will probably clear this up.

If you haven't already, read Getting Started with Rails. Look out for the discussion on MVC and scaffolding. Playing around with scaffolding can help you learn where things go and is a great place to start for beginners.
Also, I highly recommend this book: Agile Web Development with Rails. It is very hands on and an easy read.

Related

Rails models and controllers

I am new to development. I've read a few books on rails and often times they dive straight into examples. In some examples they generate resources which includes models, views, controllers etc. while in others they generate models only and vice versa. When should controllers be generated?It'd be great if someone could shed some lights to this to help me begin. Thanks.
You should do this tutorial : http://guides.rubyonrails.org/getting_started.html it pretty shows much everything about Rails concisely.
But basically ..
Controllers are files that are the first endpoint to routes. When you type an address in the navbar you end up in a controller first. Then, inside your controller an action is triggered (Show, Index, Update ...).
Actions can be blank it is not a problem. But most of the time there is some logic added. This logic can be loading some data, creating records in the database etc ...
Once the action is completed a view is triggered. This view corresponds to the controller action that has been just been visited. It is usually an ERB file that will eventually produce an HTML file after the server side scripting is completed.
Models sit alongside controllers and views: they handle all the database data. This includes validations for the fields of specific tables (does an uploaded file exceed max size, can a field of a record be blank etc ...). One database table = one model.
The most simple explanation would be :
Controllers talk to your views and models, they take requests from users (when a user is visiting your website, all requests go to the route.rb file, depending on how you set up the route.rb file the requests go to the right controller or directly to views, if you have static pages for example) and create responses.
Models are handling the hard stuff, they communicate with the database and add/remove/edit any new data in the database and provide the controller with the data it needs.
Views are just html files in which you can embed ruby code (views end with the .html.erb extension (erb is for embedded ruby)). They get the data they need from the database through controllers. The controller then sends the right views as a response to the user request.
When are you supposed to create a new view,controller or model? That's very hard to answer without an example. Every application is specific and require a good amount of experience to set up your MVC correctly.
This is the explanation from just another newbee in Rails, so don't take anything I wrote as completely true and correct.
I would suggest you to look into some books for beginners which walk you through the whole process of creating a fully functional website in Rails.
My suggestion would be to check out Michael Hartl's book The Ruby on Rails tutorial or if you want to take a step further and learn Ruby and RoR in detail, visit The Odin Project. Both are completely free and helped me a lot understand how Rails works.

General Questions about Ruby on Rails

Hey guys and merry christmas!
I'm new to ruby on rails and I'm still a little bit confused about some stuff:
When do I need to create a new controller and when not?
I want to create an app with a single searchbox and search through all of the articles. Should I create an Controller for the startpage (the searchbox) and for the search? Should I create controllers for the static pages?
Should I use an Admin interface gem or create my own?
The normal user should now have access to creating articles, just the admin. Should I use one of the admin interface gems or create my own?
Ruby on Rails follows the MVC framework, controllers are classes that contain your actions, so you need to add an action for every function your website will provide.
Technically you could have all actions in one controller but that would be just terrible, so we usually create different controllers to organize your routes and code in a better way.
Follow the Rails guide on controllers.
For the admin interface gem, you could use devise and cancan, they are both very reliable and well tested.
Ruby on Rails is indeed MVC, which means that controllers connect Models to Views. So in general it is good practice to think more resource-oriented: per resource you want found/presented, you create a controller. In your case something like:
ArticlesController : your main view, with the searchbox
PagesController : for static pages, if you need some erb/haml
admin/ArticlesController: for administration of the articles
Now, completely static pages can just as well be placed under the public folder, no need for a controller unless you need some dynamic info to be on the pages (e.g. a total count of articles).
With regards to your search-box: imho this is just a parameter for your index page. E.g. on the index you show the ten most recent articles, and when searching on some term, you show the relevant articles, but on the same controller and the same action.
With regards to the admin interface: yes, use gems like rails_admin or active_admin and it will get you started in no time at all. So definitely do that. But those gems are, of course, very general and might not suit your needs completely. It that should be the case, you can always easily revert later.
HTH.
Merry christmas!
As Khaled suggested Rails being MVC architecture it is always good to have controllers of each page. Even though you might have a static pages for now, but latter when you are trying to make the site dynamic one, then you will be in whole lot confusion of where to add a method for a particular view page.
Generally it is better to use a gem instead of making it from scratch.
You can look into this link which teaches you how to use devise and cancan with twitter bootstrap(for views). But if you are planning to learn rails then I better recommend you to do it from scratch as you will have an idea of what is happening. You can see this tutorial which does most of the task through scratch.
Enjoy Rails!!

Rails application with multiple roles

I have a rails application with 2 roles, say admin and user. But the thing is, The admin doesn't use a backend like ActiveAdmin for example. I want both Admin and User to see the same views, but depending on the role, I restrict what they can see. I'm using Cancan, but since for example both admin and user can see the product page, I end up with many conditions inside the view and controller actions stating for example if this is an admin show that, if not then show that instead.
So I don't really think that this is the "Rails way". I end up with many repeated code, and code inside the views which doesn't really support the idea of keeping the logic away from the views.
So my question is, What's the best way to implement such a scenario with many roles but the same views.
Thank you.
I'm thinking of two options currently, but I don't like either. One is to redirect the admin to another view, but this way most of the view is the same hence it's not DRY at all.
Option 2 is to use the exact same view, but add many conditions in the view, so I end up with a huge complex view with code. I'm trying to find a way that keeps things DRY yet simple, and keeps the views code free.
You can have the admin module under seperate namespace and users as the default namespace. You can extract the common code under partials and use the same in both admin and user module.
This way you can separate the code for user and admin, and if sometime in future if you decide to go for a different views for admin and user. It won;t be much of a task.
Have controllers as
app/controllers/admin/articles ------ for admin users
app/controllers/articles ---- for normal users
and
views
app/views/admin/articles
app/views/articles
app/views/shared
There are different possible approaches. A variation of the 'decorator' pattern would come to mind as described here
It's some time I last read it, but I think the basic idea is to put the logic in the model or helpers. As soon as a user logs in and you can see if he is an admin or normal user you have methods that return the necessary html or text.
A more simple approach would be to just have two partials for everything. Either as Ross says in a separate admin namespace or even simpler (but more work if you later need to change that) just like _user_view_something.html.erb and _admin_view_something.html.erb.
Somewhat similar thoughts go into the DCI pattern. This Blog gives some nice overview how this could play into Rails. The article is more about permissions, but again the basic idea is to supplement the user object with the necessary functions.
So you have the basic "do_something" method which renders something or places some information in the view and replace it with the one that fits to the role the actual user has.

Evaluation Form Creation

I'm taking a "cloud computing" course and have a few problems that I am trying to work through for the final project. I'll preface this with saying that i know nothing about this subject, and the few powerpoints I got off my online class don't really seem to point me in the right direction.
Basically what i need to do is have a page that allows a user to create a evaluation form. I have a general idea how to do it but I don't even know where to start in rails, I was hoping there was some stuff that Rails would do for me that might make it easier.
My assumption is that I could create a form that would allow entries for section headings.
Then when one occurs I need to allow actual question entries. then the user has the option of adding questions for the section. Then the user may add another heading, and questions.
When this "process" is complete, I need to allow a 2nd user to actually use the form for entries.
I'm assuming I somehow need to make a "string' object that will hold the text that I'm setting up, and then it sets up entries for each question.
I'm clueless where to start, I'll probably be asking a lot of questions. I don't want the solution I just need to get pointers, heading in the right direction.
the first step I'm thinking is finding a way to allow a "command" to do the equivalent of this but in the browser from another form:
rails generate scaffold Evaluation Topic:string rating:string comments:string
Is there a way to "create" a scaffold of a form using rails in a web browser?
It seems to me that you are mostly concerned with created forms using Rails.
Railscasts has some awesome tutorials on forms: Railscasts
After viewing some of those, you should see that Rails follows an MVC policy (more information here).
Since you mentioned the scaffold, it creates several controller actions. One of those is "new" (also "edit") which I believe would be the equivalent of creating the evaluation form.
You could then create a controller action(s) for users to fill out the evaluation form.
These links should clear up a few of your questions and get you used to the rails terminology for better questions later.
Good Luck!

Designing Web Sites with 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

Resources