Rails, One Model, Many Screens - ruby-on-rails

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.

Related

Spreadsheet-like way to add records to ActiveAdmin

I am looking for a way to add a lot of records at ones in ActiveAdmin. To be more specific, I have 2 models: Stores and Programs. Stores have many Programs.
I don't mind adding a Store using standard ActiveAdmin create view. But I would like a faster way to add programs in a spreadsheet-like way. I looked into best_in_place (https://github.com/bernat/best_in_place) but it doesn't do do adding records, just editing them.
Any suggestions? I would really appreciate it.
The short answer is Rails has nothing to help you do this. Rails has a defined convention for editing multiple objects if they belong to another object that can accept nested attributes for a few reasons, the most important of which is that there's a place to aggregate validation, as well as a standard way to differentiate each set of fields (the id). Neither of these are true during creation.
You can, however, manually work around this a couple of ways.
would be to simply write out the forms yourself, and put the logic to loop through them in your controller. This is going to be fragile, and you'll have issues getting validation to work properly, however.
would be to either create a new class that handles this single case, or try and adapt your existing Store class to handle nested attributes. There's a very solid explanation of how to do this here.

Rails Active_Admin VS. my own backend

I've been thinking of writing my own backend, because I feel active_admin might not support all the requirements.
I wanted to ask if Active_Admin supports any of these just to be sure:
I have a has_and_belongs_to_many relationship between my ad model
and tag model. In the new ad page I would like to have the form for
the ads, as well as all available tags so the admin can choose which
tags to associate with the ad. I was able to do that normally in my
application, but can I do that with active_admin?
Can I add custom buttons.. Like one to convert to PDF for example,
or one to send an e-mail..
Could I add some sort of before_filter, so the admin can only view a
model, but not edit or delete it for example?
Thank you.
All of those things can be done via Active Admin, but as it was pointed out, it can be quite a nightmare actually implementing certain things depending on the amount of flexibility you need it to have. For that exact reason, I decided to start rolling my own administration panels.
I have tried an implemented almost all robust gems for admin panels. I have also sweated over several hand-made ones.
Active-Admin is very usability centred, but it is not configuration centred.
As you rightly aniticipated, some of the more complex modifications can be tedious.
In my experience, rails_admin is the best middle ground I could find.
Take a look at it, it is highly functional, completely modular (made as a Rails 3 Engine) and simpler to modify.
If you can live without some details when customizing this is definitely the way to go. However, if you need to have everything just right, then there is not substitute for hand-made.

rails best practice for a multistep form?

I want to create a mutlistep form in Rails 3. I've watched the railscasts episode on it, but I did not feel like he was using the best practice when creating the form. I felt like it was a sloppy way to accomplish the task. What is the best way to create a multistep form using the best practices in rails?
Your question is a little hard to answer without more information, and a true answer would be a detailed tutorial (like a Railscast) rather than a SO answer, but here's some thoughts to get you on your way.
There are two major approaches to multistep forms:
Use Javascript to display the form bit by bit
Create separate views and use create/update or similar to route the user from one to the next.
There are advantages to each method, depending on whether you want to support javascript, and what your requirements are about saving data in between sections.
Advantages of 1
Faster for the user to navigate from section to section (javascript hide/show is instantaneous)
Data is easily accessible is the user wants to refer to an earlier section
Simpler controller actions
Disadvantages of 1
Will not work for users who are not running javascript (and no progressive enhancement is really possible here other than displaying the form as a huge chunk).
Will require you to provide javascript-based navigation to move from section to section (only a disadvantage if you're new to .js)
Will require AJAX if you want to save the user's information between steps.
Without AJAX and javascript, the user is at the risk of losing a lot of entry if the user accidentally pressing the back button, etc.

Symfony admin generator: To be or not to be?

on the last projects i've started, I wondered if I should use the admin generator or not. My usual choice is to not use it, as some developers suggested to me, "unless it's for quick backend prototyping and submission to client", they said. Currently i'm starting a project and i'm in the situation that the client need the backend very fast to start loading lots of data, and i'm doubting about using the admin generator or not. I would use it if needed simple form fields. But one of my models must have multiple images, and maybe I need a more complex view that allow the client to load N images, so the admin generator maybe it's not the best choice, but it seems fast, it seems to save time and that's what I need now, time saving!
The project is very simple, it's just a Product model with multiple images and multiple sizes and belongs to a simple category.
What do you think? What would be the best option for this? And where do you think that makes sense to use the admin generator or the regular module generator?
Thanks in advance!
Regards.
I use the admin generator as much as possible. It is really designed to be great for the "backend" of your website-- the administrative screens which authors and editors will use to maintain the app. Any module that needs to be user-editable and is simple cries out for the admin generator.
Recently I have been starting everything with the admin generator so that there's a working prototype to build data with. Then I select certain modules or views that need more magic sauce, and build them out with more customization.
Remember, you can add views and forms to an admin generator module. On my last project I used the admin generator for the "edit" action of my main object but added "show" methods similar to a non-admin-generator form-- adding an executeShow() action and showSuccess template.
The other thing to keep in mind is that the admin generator is only a generator. It writes a bunch of code for you in cache/frontend/env/modules, but you can override any of it by building the equivalent code in apps/frontend/modules/. If you find one part of it that you can't configure with generator.yml, you can copy the file out of the cache into your module dir and hack away. I prefer to take the "out of the box" admin generator as far as possible before customizing it, though.
i've been working with symfony for quite some time now and i've been using the admin generator for simply and complex situations. It's true that it saves time when developing CRUD modules, but i dont think that is not advisable for complex cases.
I think you should use it and also learn the power of customization the generator gives you. If you have complex Forms, leave that for form classes to manage and as you said, if your forms a quite more complex to render, well you should only take care of the rendering of that only segment of the view.
But, if you decide to make if without it, you should start thinking about creating all the view from scrap, that in my case takes quite time ( i'm not so versatile wiht css).
But this is only my opinion, hope this helps you make a more rational choice!

rails 3 - app design question - models and controllers

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

Resources