Linking actions on two tables - ruby-on-rails-3

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.

Related

using knockout with multiple views - how best to structure API

I have a knockout application I am building that has multiple viewModels in one page.
I have been mocking data so far but I am now building the API that will run it.
My question is what do I do with complex viewModels with regards to the API.
For example I have a 'add company' page that has the following tabs:-
Company info (name, address, etc.)
People at company (multiple people)
Insurance Documentation (3 different types)
I currently have each of these as separate viewModels.
When I press save I am now confused what is the best way to do things.
If I send all 3 viewModels bundled together (add them to one master viewModel) then it becomes easy to ensure that IDs all line up but that doesn't seem very modular or a good idea and breaks the idea of being 'API Centric'.
However if I send each viewModel to a different API end-point how do I ensure that the ID's match-up correctly (people -> company) etc.
At the moment I am looking at two options:-
When press 'add new' create a blank company on the database and grab its ID -> use this to match everything up.
OR -
When I press save send the new company up first, then when response comes back as (hopefully) ok then upload the rest of the information to their respective end-points using the now correct ID.
Both seem messy so I am wondering what is the 'accepted' best way to do this??
Your second approach is how I would do it, pressing Save would send the Company model first. Then once you receive the 'OK' response with the Company id, in the callback you can add it to the other models and send them up.
Nothing messy about that, just standard asynchronous behaviour.

Modelling actions in REST

I'm making a REST interface to a management system (let's call this system A) for network equipment and I have everything working. A fetches the equipment information from another backend system (let's call this system B) and index it in a database and then whenever a client asks for the information over the REST interface it returns it as JSON.
However sometimes a client needs to forcefully make A refresh some equipment information by requesting it from B (without the client being involved besides asking A to refresh it). Refreshing the information for a single node takes several seconds so the information should probably not be returned in the same request as in which it is requested. Currently we've modeled this as that you create a job of a "refresh information" type using POST /jobs with some data. But this feels very over engineered and we'd much rather want something like POST /equipment/<id>/refresh but this feels to go against the "REST way"?
Is there any other solution to this than the one mentioned with jobs above but still follow the "REST way"?
I would use GET /equipment/<id>?since=<timestamp>, where the since parameter is optional. Semantically this means:
Get the equipment with the given id as of whenever it happened to be last refreshed (when timestamp not given), or
Get the equipment with the given id refreshed no earlier than the given timestamp
Each equipment in your system would have a last_refreshed timestamp. The GET with the since=<timestamp> parameter would refresh the equipment if last_refreshed < since and then return the equipment.
A user of your service could then say GET /equipment/123?since=<15 minutes ago> and be sure they're always getting info that's no older than 15 minutes. GET /equipment/123?since=<now> means "force a refresh."
Keep in mind that POST in Rest services means that you will create some object.
To refresh one object, or update, it's recommended the PUT method.
Like um CRUD, when you have Create, Read, Update, and Delete, in REST will be POST, GET, PUT, DELETE (in order).
Maybe in your case the best URL for this is a PUT to /equipment/<id>.
URLs:
POST to /equipment/ : Create equipment
GET to /equipment/<id>: Read equipment with id <id>
PUT to /equipment/<id>: Update equipment with id <id>
DELETE to /equipment/<id>: Delete the equipment with id <id>
I hope that solve you doubt.
Anyway, a good reference it's the Apigee ebooks

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

Forms in Ruby on Rails

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.

How to decide whether to split up a VB.Net application and, if so, how to split it up?

I have 2 1/2 years experience of VB.Net, mostly self taught, so please bear with me if I seem rather noobish still and do not know some of the basics. I would recommend you grab a cup of tea before starting on this, as it appears to have got quite long...
I currently have a rather large application (VB.Net website) of over 15000 lines of code at the last count. It does not do retail or anything particularly complex like that - it is literally just a wholesale viewing website with admin frontend, catalogue / catalogue management system and pageview system.
I don't really know much about how .Net applications work in the background - whether they are all loaded on the same thread or if each has its own thread... I just know how to code them, or at least like to think I do... :-)
Basically my application is set up as follows:
There are two different areas - the customer area and the administration frontend.
The main part of the customer frontend is the Catalogue. The MasterPage will load a list of products but that's all, and this is common to all the customer frontend pages.
I tend to work on only one or several parts of the application at a time before uploading the changes. So, for example, I may alter the hierarchy of the Catalogue and change the Catalogue page to match the hierarchy change whilst leaving everything else alone.
The pageview database is getting really quite large and so it is getting rather slow when the application is first requested due to the way it works.
The application timeout is set to 5 minutes - don't know how to change it, I have even tried asking this question on here and seem to remember the solution was quite complex and I was recommended not to change it, but if a customer requests the application 5 minutes after the last page view then it will reload the application from scratch. This means there is a very slow page load whenever it exceeds 5 minutes of inactivity.
I am not sure if this needs consideration to determine how best to split the application up, if at all, but each part of the catalogue system is set up as follows:
A Manager class at the top level, which is used by the admin frontend to add, edit and remove items of the specified type and the customer frontend to retrieve a list of items of the specified type. For example the "RangeManager" will contain a list of product "Ranges" and will be used to interact with these from the customer frontend.
An Item class, for example Range, which contains a list of Attributes. For example Name, Description, Visible, Created, CreatedBy and so on. The form for adding / editing loops through these to display relevant controls for the administrator. For example a Checkbox for BooleanAttribute.
An Attribute class, which can be of type StringAttribute, BooleanAttribute, IntegerAttribute and so on. There are also custom Attributes (not just datatypes) such as RangeAttribute, UserAttribute and so on. These are given a data field which is used to get a piece of data specific to the item it is contained in when it is first requested. Basically the Item is given a DataRow which is stored and accessed by Attributes only when they are first requested.
When one item is requested from a specific manager is requested, the manager will loop through all the items in the database and create a new instance of the item class. For example when a Range is requested from the RangeManager, the RangeManager will loop through all of the DataRows in the Ranges table and create a new instance of Range for each one. As stated above it simply creates a new instance with the DataRow, rather than loading all the data into it there and then. The Attributes themselves fetch the relevant data from the DataRow as and when they're first requested.
It just seems a tad stupid, in my mind, to recompile and upload the entire application every time I fix a minor bug or a spelling mistake for a word which is in the code behind (for example if I set the text of a Label dynamically). A fix / change to the Catalogue page, the way it is now, may mean a customer trying to view the Contact page, which is in no way related to the Catalogue page apart from by having the same MasterPage, cannot do so because the DLL is being uploaded.
Basically my question is, given my current situation, how would people suggest I change the architecture of the application by way of splitting it into multiple applications? I mean would it be just customer / admin, or customer / admin and pageviews, or some other way? Or not at all? Are there any other alternatives which I have not mentioned here? Could web services come in handy here? Like split the catalogue itself into a different application and just have the masterpage for all the other pages use a web service to get the names of the products to list on the left hand side? Am I just way WAY over-complicating things? Judging by the length of this question I probably am, and it wouldn't be the first time... I have tried to keep it short, but I always fail... :-)
Many thanks in advance, and sorry if I have just totally confused you!
Regards,
Richard
15000 LOC is not really all that big.
It sounds like you are not pre-compiling your site for publishing. You may want to read this: http://msdn.microsoft.com/en-us/library/1y1404zt(v=vs.80).aspx
Recompiling and uploading the application is the best way to do it. If all you are changing is your markup, that can be uploaded individually (e.g. changing some html layout in an aspx page).
I don't know what you mean here by application timeout, but if your app domain recycles every 5 minutes, then that doesn't seem right at all. You should look into this.
Also, if you find yourself working on various different parts of the site (i.e. many different changes), but need to deploy only some items in isolation, then you should look into how you are using your source control tools (you are using one, aren't you?). Look into something like GIT and branching/merging.
Start by reading:
Application Architecture Guide