Reputation: 112
I've been on this for over a day now and I hope someone can help me out. I'm new to ruby/rails and I think I'm getting close.
I have a view whereas you can input basic information about an apartment such as unit number, rate, if it is enabled. You can then enter how many apartments you want to create. This information is placed into session variables and a preview form is shown. So for example if you had entered 5 for the number of apartments you wanted to create you would get 5 rows in a table, each with a heading of Unit Number, Rate, and Enabled. You could then make modifications as needed to any individual apartments and click on Save.
/app/models/apartment.rb
def preview
@count = session['count'].to_i
@mktg_apartment = []
1.upto(@count) do |i|
@mktg_apartment[i] = Apartment.new
@mktg_apartment[i].rate = session['tmp_rate']
@mktg_apartment[i].enabled = true
end
end
/app/views/preview.html.haml
=form_for @mktg_apartment, :url => {:action => 'save_preview', :method => 'post'} do
- 1.upto(@count) do |x|
text_field "mktg_apartment[#{x}]", :unit
text_field "mktg_apartment[#{x}]", :rate
text_field "mktg_apartment[#{x}]", :enabled
I don't really have my save method created yet. I will put out what I have, at one point it would save only the last record entered, but I know it's all wrong at this point.
/app/model/apartment.rb
def save_preview
@mktg_apartment = Mktg::Apartment.new(params[:mktg_apartment])
respond_to do |format|
if @mktg_apartment.save
format.html { redirect_to mktg_apartments_path }
else
format.html { render action: "preview" }
end
end
end
In my preview.html.haml form I dumped @mktg_apartment and see the following:
[nil, #<Mktg::Apartment id: nil, created_at: nil, updated_at: nil, unit: nil, rate: 1222.0, enabled: true>,
#<Mktg::Apartment id: nil, created_at: nil, updated_at: nil, unit: nil, rate: 1222.0, enabled: true>,
#<Mktg::Apartment id: nil, created_at: nil, updated_at: nil, unit: nil, rate: 1222.0, enabled: true>,
#<Mktg::Apartment id: nil, created_at: nil, updated_at: nil, unit: nil, rate: 1222.0, enabled: true>,
#<Mktg::Apartment id: nil, created_at: nil, updated_at: nil, unit: nil, rate: 1222.0, enabled: true>]
When I hit save it errors out and I see the following parameters being passed to my post form:
{"utf8"=>"✓",
"authenticity_token"=>"iGLdu/ZmulcLp4xZEU4a4iuxWV1opzNIyXcnZEQCtj8=",
"mktg_apartment"=>{"1"=>{"unit"=>"",
"rate"=>"",
"enabled"=>"0"},
"2"=>{"unit"=>"",
"rate"=>"",
"enabled"=>"0"},
"3"=>{"unit"=>"",
"rate"=>"",
"enabled"=>"0"},
"4"=>{"unit"=>"",
"rate"=>"",
"enabled"=>"0"},
"5"=>{"unit"=>"",
"rate"=>"",
"enabled"=>"0"}},
"commit"=>"Save",
"method"=>"post"}
So it looks like however I'm setting up the array of @mktg_apartment in my preview method is not being accessed correctly in my preview view. It actually looks like my preview view is passing the parameters as I would expect to the save_preview post method.
Any help please, I'm beating my head against the wall. Thank you...
Upvotes: 0
Views: 1056
Reputation: 5126
I would use a nested form. Split the units into a separate model and have something like block of land (block) has many units. Then you could enter all the information using a complex form.
Refer: http://railscasts.com/episodes/196-nested-model-form-part-1
The block model could hold the size of the entire block and a unit model can hold the particulars about the specific unit.
Upvotes: 1