Forms in Ruby on Rails - ruby-on-rails-3

Ive recently started using Ruby on Rails for a project of mine and have hit some interesting walls. Im using scaffolding but instead of moving to the default show page after i have entered data, i want to instead redirect to another page i have created.
The key is the page is another form and half of that form is made up of values entered in the previous form. So ive guessed that i have to keep that in memory instead of allowing it to be written to the database. My original idea was to query the table and find the last record but with multiple users its probably not advisable.
Can anyone help? Examples would be great or even just to point me in the right direction
Im pretty familier with C++, jave etc, so i understand computer jargon.
thanks

Do you care if the data from the first form is stored in the database before going to the second form? If not you can just set the create action in the controller to redirect to the new page you want. Depending on how you are managing the user session, you could then pull up the information the user stored in the first form. For example if you were using Devise to handle the session, you could have the user enter their username, email and password in the first form, then redirect to a profile form that has a separate email option. You could pre-populate the field to show the user.email, but give the user the option to create a separate email for their profile. In your new action for your profile you would just add #profile.email = current_user.email. Keep in mind, this will allow the user to edit this field separate for both tables.
If you want the data entered in the two forms to point to the same database table, I would recommend looking at using nested attributes. This way, you do not have an email columns in separate tables. Railscasts has a pretty good episode on how to do this. http://railscasts.com/episodes/196-nested-model-form-part-1

You can store all entered in the first form data in session. After redirect you can fill all form inputs from stored data in session and then clear that session data.

Related

Prevent duplicated form submission MVC 4

I know this has been asked several times may be, but most (if not all) solutions I've seen were involving a javascript solution or disabling form/submit button, which I'm not preferring.
I have couple of huge forms that may take some time to complete submission which may let the user to re-submit many times during the first submission, which eventually will cause for many records in the database.
Proposed solution:
A proposed solution is to have a generic method that have an array including all submitted forms IDs, each time a user submit a form, it's ID will be added to that array if it's not already existed, after the submit complete the ID will be popped out the array. A drawback can be counted on this solution in case a user opened two or more pages of the same form and submit them synchronously, but that can be worried about later.
My question is how feasible is my proposed solution and how good it can be? and how can I implement that generic method?
I understand that Apache Struts 2 framework has some solution to prevent such thing (Token Interceptor), how to do that in MVC 4?:
Just add a GUID field to the ViewModel the view is bound to (You are using ViewModels right?) and every time the empty form is asked for, generate a new GUID (solves the issue of downloading two or more copies by same user) for it and have it included in the view as a hidden field (string representation of it). Then you can implement the array solution you are talking about in a session variable or even a database based solution in which all processed GUIDs are saved in a table that gets cleared every so often, a text file that contains the list of GUIDs, etc. kind of hard to pick a complete solution based on just what you provide. But the above should provide you with all the requirements you have outlined.

Creating a form to send multiple emails to existing emails using Rails

I'm building an order management system for customers. I need to set it up so I can build a form that emails a brief of the order status to a customer. The trick is that a customer can have multiple emails i.e no limits on that and the form I need to set up would show the brief generated in a textbox(nothing hard there) as well as a list of checkboxes with the email addresses which to send to.
Clicking on/off the checkboxes determines which addresses would the brief be sent to. I have everything worked out except for one main thing i.e the form - I'm not sure how can I actually set this part up i.e I'm using simple_form here and I'm not sure which model would I be making the form against? Do I need to build another model here? I'm pretty stuck.
May be you can create model like Email without database and use all ActiveRecord helper methods. These articles may be helpful.
http://yehudakatz.com/2010/01/10/activemodel-make-any-ruby-object-feel-like-activerecord/
http://railscasts.com/episodes/193-tableless-model?view=asciicast

Linking actions on two tables

Sorry the title isn't very descriptive, but here is what I'm trying to do:
I am making a website that handles student group funding requests. A particular request for funding could involve any number of items. I have two tables: requests and items. A group begins a request by filling out a "request" form, which contains a brief summary of the request, a category (speaker, advertising, etc.), and some other fields necessary for the bureaucratic process. That works fine and was handled pretty much entirely by scaffolding. The problem I'm having is with the "items". I would like the user to click the "submit" button on the new request form and be taken to another page at which he can add any number of items to the items table, all containing in a field the id of the request he just submitted.
The front end appearance doesn't really matter - it could all be on one page - what's important is that the user submits information to one table and then submits information to a second table, with the info sent to the second table containing something that connects it to the information just submitted to the first table.
As you can probably guess, I'm pretty new at rails (I went through the book Head First Rails and I'm working on my first project), so I'm looking for a general explanation about how this can be accomplished.
I recommend you to read / watch this tutorial. It is quite good.
UPDATE:
http://railscasts.com/episodes/196-nested-model-form-part-1
Hope it helps.

How do I setup a Rails view without knowing specifics about the form_for?

I'm building a workflow for a Rails app in which a user logs into the system, and on the home page to the app a supervisor selects a student and a tutor who the student spent time. Then they submit the form. This should all happen on the app's home page. My problem is since it is not known when you go to that page who the specific student is, and thus who this form is for, it's not obvious to me how to setup the view and controller for this page.
Perhaps the form is for the lesson, but I've got a document (MongoDB) data model in which the lesson is embedded within the student document.
I could imagine a workflow where the user sees a list of students on the home page, and then clicks on the student to go to the model/controller for that specific student, and then enters the lesson's info there, but I'm trying to avoid unnecessary clicks and screens if possible.
Is my data model wrong? Is there a way to build this and keep my current data model? Is there a Rails pattern or helper that I should be aware of?
Thanks,
Bob.
When you build a workflow, you usually have one of your controllers behave as an orchestrator, and that controller sends the user to the right page.
Since you only want one form to send a user to 2 potential pages, you could post the form to a controller (a controller called "search" for example), and that controller decides weather to redirect the user to one of the two views, with the necessary parameters in the url (such as student_id, tutor_id or lesson_id).
And please work on your accept rate, you won't get many responses otherwise.

Prevent write to SQL until admin approval

Gotta question. On my website I have a registration form that, once submitted, writes the data to a database. The address that the user provides is used to display their information on a google maps (city, state, country... not SPECIFIC location). Everything shows up on google maps instantaneously, but I'm worried that something will show up on there that shouldn't.
Say someone submits something on the form... like first_name "Seymour" last_name "Butts". Obviously, I don't want a marker added that shows Seymour Butts' location.
Is there anyway to submit a form, have it sent to a designated admin for approval, and, once approved, THEN have it write to the sql database? I think that'd be the best way to do it... I'm open to suggestions!!!!
Add a field to whatever table you write to that designates whether the item has been approved or not. You can then query the table for blanks to see which items need to be approved, and query for approved items to only show non-Butts items.
You could add it to an intermediate table, say a "WaitingForApproval" table.
Then you could write an admin tool listing the contents of this table with each item having an "Approve" button next to it, which would trigger the insert into the real table.