views:

49

answers:

1

Hello,

Just thinking about the best way to build an Order form that would (from user-perspective):

  • Allow adding multiple items.
  • Each item has Name, Job Type drop-down and File upload.
  • User can add or remove items.
  • User must provide at least one item.
  • All the items should be validated (eg: Name, JobType and File are required).
  • When he hits the Submit, an Order should be created with all the items in it.

So the model looks like: User -1---*-Order-1---*- OrderItem-*--1-JobType. Additionally OrderItem includes number of attributes, let's say name, file.


Now I would like to have as skinny controller as possible. Better with the 'standard' code like:

class OrdersController < ApplicationController
  def create
    @order = Order.new(params[:order])
    if @order.save
      redirect_to account_url
    else
      flash.now[:error] = "Could not save Order"
      render :action => "new"
    end
  end
end

The most interesting thing is how a view should look like so that rails automatically binds all the values posted to the correct objects?

For now I don't worry about degradation from JavaScript, but rather the template for the items to be added and linking all that with the validation (probably as a partial).

I expect a template to look like the one below, but not sure.

<input type='text' name='order[order_items[name[]]][]' />
<select name='order[order_items[job_type[]]][]'>...</select>
<input type='file' name='order[order_items[file[]]][]' />

I have to clone this template in order to add item using JS.


So the answer should:

  • Include the view or part of it that demonstrates:
    • correct binding of the post values;
    • correct validation for all items;
  • Include related part of model.
  • Include changes to the controller IF and ONLY IF necessary.
  • (JavaScript is not required)

Ideally I would like to see a sample if somebody knows one (maybe somewhere on GitHub).

And the last note, please, check your solutions before posting.

Thanks,
Dmitriy.

+1  A: 

Not answering the question directly, but here are some sources to get you there:

On a personal note. Your question comes across as asking for someone to do your work for you. I would gladly post an answer to cover all your needs if I was on the clock.

Tony Fontenot
Tony, I am sorry for giving the feeling that I want somebody to do the work for me. I haven't had such intention. I just wanted to avoid possible speculations on the answer and just get the clear ones. Such an answer is yours. It is clear as it really provides exact answer I expect - the railcasts 196 (and 197). Thanks a lot.
Dmytrii Nagirniak