Repeating form

I have a controller that creates a new sale
def new
@bakery_output = BakeryOutput.new
end

GET /bakery_outputs/1;edit

def edit
@bakery_output = BakeryOutput.find(params[:id], :include =>
[:recipe, :customer])
end

POST /bakery_outputs

POST /bakery_outputs.xml

def create
@bakery_output = BakeryOutput.new(params[:bakery_output])

respond_to do |format|
  if @bakery_output.save
    flash[:notice] = 'Sale was successfully created.'
    format.html { redirect_to bakery_output_url(@bakery_output) }
    format.xml  { head :created, :location =>

bakery_output_url(@bakery_output) }
else
format.html { render :action => “new” }
format.xml { render :xml => @bakery_output.errors.to_xml }
end
end
end

and a view to go with it

New Sale<%= error_messages_for :bakery_output %> <% form_for(:bakery_output, :url => bakery_outputs_path) do |f| %>

 
Customer Recipe Qty Date Batchcode
<% @customers = Customer.find(:all, :order => "company")%> <%= f.collection_select(:customer_id, @customers, :id, :company) %> <% @recipes = Recipe.find(:all, :order => "name")%> <%= f.collection_select(:recipe_id, @recipes, :id, :name) %> <%= f.text_field :sales_quantity, :size => 3 %> <%= f.date_select :saledate %> <%= f.text_field :salebatchcode, :size => 10 %>
<%= submit_tag "Create" %>
<% end %> <%= link_to 'Cancel', bakery_outputs_path %>

I want to be able to initiially have the ability to have 5 sales, saved
to the database, and after that to make it ajax friendly using partials
etc. I want to do this 1 stage at a time as I am new to rails and want
to try to understand more fully what is going on. So my question is how
do I achieve the 5 sales from a display and controller point of view.
thanks
Martin

Martin E. wrote:

I want to be able to initiially have the ability to have 5 sales, saved
to the database, and after that to make it ajax friendly using partials
etc. I want to do this 1 stage at a time as I am new to rails and want
to try to understand more fully what is going on. So my question is how
do I achieve the 5 sales from a display and controller point of view.
thanks

Not sure exactly if this is what you want but it sounds similar and may
be the starting point you need:

Mark B. wrote:

Martin E. wrote:

I want to be able to initiially have the ability to have 5 sales, saved
to the database, and after that to make it ajax friendly using partials
etc. I want to do this 1 stage at a time as I am new to rails and want
to try to understand more fully what is going on. So my question is how
do I achieve the 5 sales from a display and controller point of view.
thanks

Not sure exactly if this is what you want but it sounds similar and may
be the starting point you need:

#74 Complex Forms Part 2 - RailsCasts

Yes thanks I’ve seen that and could see how it worked where there was a
one to many relationship between project and tasks. I tried implimenting
some of the ideas and failed miserably. It’s the initial part I need
most assistance with I think after that I would be able to apply the
knowledge from the screencasts.
thanks
Martin

Ah okay.

So in your controller, in the new action, add in @sales = [Sale.new] * 5

In the view, loop over them like this:

<% for sale in @sales %>
Product: <%= text_field ‘sale[]’, ‘product’ %>
Amount: <%= text_field ‘sale[]’, ‘amount’ %>
<% end %>

I’m guessing you’ve got product and amount… and you want them stored
as text… you could have anything… the important thing is the []
specifier

Then in your receiving controller action (create), you need to loop
through, updating them all using a special syntax

Sale.update(params[:sale].keys, params[:sale].values)

Julian.

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)
VIDEO #3 out NOW!
http://sensei.zenunit.com/

Hi,

On 10/04/2008, at 6:59 AM, Martin E. wrote:

Yes thanks I’ve seen that and could see how it worked where there
was a
one to many relationship between project and tasks. I tried
implimenting
some of the ideas and failed miserably. It’s the initial part I need
most assistance with I think after that I would be able to apply the
knowledge from the screencasts.
thanks
Martin

These are the steps:

Create a rails project
Configure your database
Create a controller called sales. Use script/generate controller sales
Create an action in the sales controller called index
Create a model called Sale. Use script/generate model Sale
Fill in the migration (it’s in db/migrations folder) to include at
least an attribute called name (for our demo)
in the index action, add the following line:
@sales = Sale.find(:all)
now, in your views volder, go into the sales subfolder, and create a
file called index.rhtml
in that file, add the following:

<% for sale in @sales %>

Name: <%= sale.name %>

<% end %>

this will loop through all the sales, printing out their name.

If you need help with any of those steps let us know.

Now, what model do you want to assocation to sales? Then we can go
from there.

Julian.

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)
VIDEO #3 out NOW!
http://sensei.zenunit.com/

Julian L. wrote:

Ah okay.

So in your controller, in the new action, add in @sales = [Sale.new] * 5

In the view, loop over them like this:

<% for sale in @sales %>
Product: <%= text_field ‘sale[]’, ‘product’ %>
Amount: <%= text_field ‘sale[]’, ‘amount’ %>
<% end %>

I’m guessing you’ve got product and amount… and you want them stored
as text… you could have anything… the important thing is the []
specifier

Then in your receiving controller action (create), you need to loop
through, updating them all using a special syntax

Sale.update(params[:sale].keys, params[:sale].values)

Julian.

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)
VIDEO #3 out NOW!
http://sensei.zenunit.com/
Thanks for your help I think that will assist me a lot, but what do I
use for the form notation

regards
Martin

Julian L. wrote:

Just what you’d usually use.

Julian.

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)
VIDEO #3 out NOW!
http://sensei.zenunit.com/
Hi I’m still struggling with this
what should my form_for line say

thanks for any help given
regards
Martin

Just what you’d usually use.

Julian.

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)
VIDEO #3 out NOW!
http://sensei.zenunit.com/