rails 3 - app design question - models and controllers - ruby-on-rails

I am building small app that will only display products in various categories. And will never display categories without products.
So far I have two models - product and category and wondering if I really need controller dedicated to category model? I can only see one advantage so far - rendering collection (partial) of category. But it could be done via product as well. I want to keep the code as small as possible. Just wondering what is the best approach in such situation, what about routing and resources in rails 3?
Thanks a lot for any suggestions.

I think you should keep the controller for the following reasons:
1) Maintenance of categories, basic CRUD functionality may need to be implemented so this will be required.
2) If anyone else has to maintain the code at a later date it is much easier for them if all the basic details are as expected. Finding a controller missing would probably start to raise a developers suspicions as to what other oddities there will later uncover.
3) How much smaller will not including the controller make it? Its not going to be a vast difference and so for clarity it is probably best to include it.

don't you will need categories controller to add a new category or delete an unused one?
trying to "keep the code as small as possible" at earlier steps often makes your code messed-up at later development steps.

Still in the same topic and CRUD.
It looks like I will not need CRUD for Category model. I am not going to display or manage it as it will be pure static data (still in db) but seeded only once. Therefore what benefit Category Controller would give?
In my app I will only display products inside category - will it be possible without having Controller for Category - I am thinking of valid URLs like /Category/1/Product/1 or even simplifying it to Product/1, Product/2 but still Category is used to navigate between categories.
Any advice or examples?
Thanks

Related

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

Modifying Rails: How do advanced users find out what needs to be changed?

I've been using Rails for a few months now, and I'm quite comfortable writing up a project & manipulating Rails to my needs, etc.
Recently I've been trying to get a little more advanced, so I've attempted to modify/add to the existing rails codebase: add new form helper methods, add a responds_to :pdf method, etc...and I've been having a lot of problems.
The difficulty is learning which code I need to modify; where that code is located, and how to ensure I don't miss related code in other files. I'm guessing there's a way people learn to do this, but at the moment I'm mostly just guessing-and-hoping.
I guess my question is, how do Rails folks go about learning where the code they need to modify is edited & the approach to editing it? It seems like it's just something you need to know from prior familiarity, but I'm guessing there has to be a simple method for understanding where (and what) to edit.
Any ideas appreciated...cheers
I highly recommend Jose Valim's Crafting Rails Applications
You go through advanced projects, building out the types of engines and customizations that will take you to the next level in your Rails development.
From the site:
This book will help you understand Rails 3’s inner workings, including
generators, template handlers, internationalization, routing, and
responders.
What you are asking for is how MVC works. Basicly you can say:
1.) Put logic to the model! The model is the pivot everything turns around.
2.) The Controller is a middleman between the model and the view. You dont put any logic here that isnt related to selecting data from the database that should be displayed in the view. If you use one selection logic more than once create a scope in the Model and use it in the Controller.
3.) The View is only there to display things! You dont put any logic here! All the logic comes from the model and the data comes from the controller. The only logic your using here are loops through arrays of data that should be displayed.
Then you have some things missing. If you have a task that is related to an external service like lets say a SOAP Service you write a class for that too! Just whithout using ActiveRecord::Base inheritance like its generated by the scaffolder. You can call this Class in other models. Dont put this to the controller or copy the code in every class that needs it! Stay DRY (Dont Repeat Yourself). Just write a class for it and include it in the other models!
Another thing thats a Database basic: Dont store data that could be calculated from other fields from the database! You can add methods that calculate the stuff you need but dont start with duplicates.

Ruby on Rails - Create Entity with Relationship

I'm new to rails, so be nice.
I'm building a "rolodex" type application, and this question is about the best way to handle creating an entity along with several relationship entities at the same time.
For (a contrived) example:
My application will have a Person model, which has_one Contact_Info model. On the create.html.erb page for Person it makes sense for the user of my appliction to create the person, and the contact_info at the same time.
It doesn't seem right to include details for creating a contact directly in the create view/controller for person. What's the rails way to handle this?
Using nested attributes is the most common way to do this.
The actual documentation is here.
You want to use "Nested Forms". There is a great example of them in this blog post.
I'm also noob, but I had a similar issue with an app. I was using a tutor at the time and he basically said it was a good example of rails being opinionated. It sounds like you want to take the create action for two different models at the same time, which may be possible but probably very hard. Id suggest considering whether your data model could be modified, or find a way to make an acceptable user flow while collecting the data in different forms.
Update: while writing this the technical answer came in. Keep in mind, its perfectly okay to take the easy route if doing so helps you get the app out the door, and especially while you're still new.

Rails, One Model, Many Screens

I have a model with many fields (nearly 40). The client wants the fields divided among multiple screens. The model also has a few has_manys that should look like they are part of the same model.
How can one divide the model, and what are the tradeoffs among the ways to do it?
I see a couple of possibilities:
1) Use JavaScript to show and hide parts of the form. I think I can make that one work.
2) Use forms that submit to different actions. Can form_for be used with appropriate options?
I'm looking for other ideas too.
Check out acts as state machine. You can use this to create wizards and whatnot.
Having reread your question, I think Javascript is really what you're looking for. Check out jQuery UI, they've got a tab component that will probably help.
I would check out the ActsAsWizard plugin. Makes doing a wizard like this extremely simple.
Check out the readme it is excellent.

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