So I'm creating a simple application. I have a 'Games' database table and a 'Rules' database table. Each game can have many rules. I want to present a form like this:
Game Title:
Game Description:
Rule #1:
(Click to add another rule)
So the user can click a button to add another text field for additional rules. What is the best way of going about this? Are there form helpers? Any way to return an array of rules? I'm somewhat new to Rails, and my googling didn't provide much help. I may just be being ignorant, but any help would be greatly appreciated!
What you are looking for are called "dynamic forms" and the answer to your question is in the following Railscasts episode.
http://railscasts.com/episodes/403-dynamic-forms
https://github.com/railscasts/403-dynamic-forms
Its kinda late but i hope it helps someone else.
This is old but should be identical to what you are trying to do at the core.
http://railscasts.com/episodes/75-complex-forms-part-3
Just think of Project and Tasks to your Game and Rules.
Use javascript. This would help
http://railscasts.com/episodes/197-nested-model-form-part-2
You'll likely want to use a "game" form with nested "rules" forms. This would be a lengthy answer if I put all the code needed but visiting these should help you right along:
http://railscasts.com/episodes/196-nested-model-form-part-1
http://railscasts.com/episodes/197-nested-model-form-part-2
Related
It's just a question.
Is it possible to set a text template on edit for Rails-Admin?
Whenever inputting some data, it takes a long time.
So, I think if I can call like a mail template, it would takes a short.
I searched that kind of the function for Rails-Admin but I couldn't find it..
Any ideas?
Rails admin does not have anything special that can help you with this. The edit view, the closest thing that could help you, is auto-generated from the model fields and associations. And unless you want to add a model for each template you probably should steer clear of this approach.
What you need is to build a custom action. You'd have to practically build everything like a regular controller.
Check out the actions page of the rails admin wiki for instructions on how to do this.
Good luck
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
Is there a way to allow admins to modify the content of simple static pages?
I know I can create a "Pages" model with attributes like "Title", "Content", "Images" etc, and make administrators modify them. But is there a gem for such a thing?
I found a different kind of solution which offered me a more completed way in modifying static pages.
Since pages could be different than simple text I used Mercury editor. Watch the demo in the page. There is a good railscast about that but don't follow it too strictly since something is changed in the new versions(for example the update method is saveUrl while in the cast is called saveURL).
Watch out!: the github project is this, there is another one on github but it didn't work for me.
Hope this will help somebody.
Copycopter was created for this sort of thing. Watch the railscast.
I have to add Wufoo-like WYSIWYG form-builder functionality to a Rails webapp.
Does anyone know of good resources (gems/engines/plugins/example code) that would help?
this is not really an answer to your question, but I still can't add comments unfortunately, due to my reputation level, sorry :)
There is exact equivalent of such functionality in Drupal(php)
http://drupal.org/project/webform especially useful for contact forms, i.e. clients happy and don't bug me every time they want to adjust or even to add new inquiry form :)
Would be nice to have such gem/plugin if any? :P
Thanks.
I don't think creating such a app in rails would be a great idea.
Using AR, such an app would be creating migrations on the fly - which doesn't sound like a great thing to do.
AFAIK, wufoo uses php.
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.