Question

I am new to web development (I do have several years of experience in server side). I need to create a page where one enters order information. For example: Item Name, Quantity, Price Then one can click an "Add" button and another line of input will appear and the user can fill that. In the end the user clicks "Submit" and the order is processed.

Now I know how to make a winforms application that does that, but I am new to rails, and in general to web development, could someone please point me to the right technology/package?

Perhaps someone has a link to a relevant tutorial? (I have done most of the rails tutorial but did not see anything like that there)

Thank you!

Was it helpful?

Solution 2

You want dynamic nested model forms.

Here is a gem that makes it work out of the box

Note: you will need to use jquery or prototype javascript frameworks.

One of the most common gotchas:

Make sure the parent model has attr_accessible :children_attributes.

Example: School has_many :students, you would need attr_accessible :students_attributes in your School model

You can read up on this and more at RailsAPI page for accepts_nested_attributes_for

OTHER TIPS

You need to have an Order model and OrderItem model (name, quantity, price) for individual order items. Order model has many to one relationship with OrderItem. Order accepts nested attributes for order items. Create a form for Order together with fields for OrderItem attributes. For the Add button functionality use Cocoon. All you need to do now is to save it in create action.

Cocoon's homepage contains several examples that you can adapt to your problem - project/tasks is the same as order/order_items relationship. Also take a look at #196 Nested Model Form Part 1 that discuss similar topic.

If not solve your problem, this should hopefully give you something to Google for.

http://railscasts.com/episodes/196-nested-model-form-part-1
http://railscasts.com/episodes/197-nested-model-form-part-2

These 2 screen casts probably almost identical to what you are trying to accomplish, just different context. They are old, but the basics still apply. I would probably approach the javascript portions differently these days.

If you are new to rails and want to get quickly into it, then check this guide:

http://guides.rubyonrails.org/getting_started.html

The point 5 (Generate a Scaffold) fits your needs.

After creating a new rails application you could run:

rails generate scaffold Order name:string quantity:string price:integer

This generates you Controller, Model and Views related to the ordering process.

Run

rake db:setup
rake db:migrate

to prepare the database

Now everything should be fine and you got a page where the user can enter the order information and save it (witch should fit your start needs). Start the Server with

rails s

and check it out.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top