# Problems Processing multiple form elements generated by javascript actions

**URL:** <https://www.ruby-forum.com/t/problems-processing-multiple-form-elements-generated-by-javascript-actions/176794>\
**Category:** Rails\
**Created:** [October 6, 2009, 10:03am UTC](https://www.ruby-forum.com/t/problems-processing-multiple-form-elements-generated-by-javascript-actions/176794 "2009-10-06T10:03:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [October 6, 2009, 10:03am UTC](https://www.ruby-forum.com/t/problems-processing-multiple-form-elements-generated-by-javascript-actions/176794/1 "2009-10-06T10:03:12Z")

</div>

Hi all,

After many days of struggling, I have a multi-model form with ajax  
elements more or less working, but I’m hitting a wall with a few bugs  
that I can’t figure out. Guidance would be very much appreciated.

I’m using the Ryan B. technique from Advanced Rails recipes to  
dynamically add and remove elements on a multi-model form.

> **[multiple\_models\_one\_form.pdf](https://media.pragprog.com/titles/fr_arr/multiple_models_one_form.pdf)**
>
> 111.40 KB

I have 3 models: users, schedules and markets.  
Users  
has\_many :schedules  
has\_many :markets, :through =\> :schedules

Markets  
has\_many :schedules  
has\_many :users, :through =\> :schedules

Schedules  
belongs\_to :users  
belongs\_to :markets

Schedules has columns user\_id and market\_id, but also has additional  
columns: monday, tuesday, wednesday, thursday, friday, saturday,  
sunday. These are booleans.

All the editing in my app happens from the user model. The list of  
available markets are prepopulated in the app, and the user cannot add  
new ones. On the user#edit view, I’m showing the user a dropdown for  
each of his existing markets and for each market, I’m rendering 7  
checkboxes representing each day of the week. The goal is that each  
market selected and its associated selected dates should be saved as a  
“schedule” record in the schedules table. The row in that table should  
contain a user\_id, a market\_id, and each selected day of the week as  
true. When a user removes a market from the edit page, then that  
schedule row should be destroyed.

As discussed in Ryan’s tutorial, there are issues regarding new vs.  
existing data. I’m running into one of these. I’m also having problems  
with deleting records. I’m going to explain my issues and then lay out  
my full set up below.

New vs. existing data: The schema I explained above is mostly working.  
However, I’m having issues when the user selects a new market AND  
selects some checkboxes to indicate the days he attends. Using the  
technique described below, I have an “add new schedule” link that  
dynamically generates a market dropdown along with seven checkboxes  
for the dates. This works fine. I can select one of my markets and  
check off some days. However, when I click update, these inputs save  
as multiple records in the schedules table. The market\_id, user\_id and  
monday=false save as one row. Then, for each checkbox that I selected,  
the user\_id and day save as a row. So if I select 3 days, then I get 4  
new records: one for the market and one for each data checked.

Here’s the part about new vs. existing data: When I go back to the  
edit page, I see the correct market in one dropdown with no checkboxes  
selected and then one additional empty dropdown with one checkbox  
selected for each checkbox that the user selected before hitting  
update. Now that the schedule is an “existing” record, when I check  
off some days associated with the dropdown where the market is  
selected, and I hit “update”, these save perfectly and render  
correctly on the user#show page. So basically, the edit/update action  
is working perfectly and the creation of new schedules is not. The  
problem is really just the checkboxes because the market actually  
saves correctly. One solution I tried was to add something like the  
following to each check\_box method, but this blew up the app: :index  
=\> (showing.new\_record? ? ‘’ : nil). This is problem #1.

Problem #2 is that when I click the “remove” link for a given schedule  
(i.e. a market and its 7 available checkboxes combination, and I hit  
update, the user\_id and market\_id are properly deleted from the  
schedules table. However, the record itself is not deleting and the  
boolean fields representing the days of the week remain as well. I’m  
able to hack around this and technically the app works because these  
half-empty rows aren’t associated with any of my models, but I’m  
quickly building up a database table filled with orphaned, useless  
data. Ideally, when I delete a “schedule” record, I’d like it to be  
destroyed.

The following is my setup. You’ll see that I’m following Ryan’s  
tutorial very closely. One area where I depart from it is the  
checkboxes, which I implemented based on the Rails API fields\_for  
method examples.

User#Edit view:

\<%= error\_messages\_for :user %\>  
\<% form\_for @user do |f| %\>  
\<%= add\_schedule\_link “+ Add another market” %\>

  
\<%= render :partial =\> ‘schedule’, :collection =\>  
@user.schedules %\>

 \<%= f.submit 'Update My Profile' %\> \<% end %\>

User#\_schedule.html.erb

 \<% new\_or\_existing = schedule.new\_record? ? 'new' : 'existing' %\> \<% prefix = "user[#{new\_or\_existing}\_schedule\_attributes][]" %\>

\<% fields\_for prefix, schedule do |schedule\_form| -%\>  
\<%= error\_messages\_for :schedule, :object =\> schedule %\>

\<%= schedule\_form.collection\_select :market\_id,  
Market.all, :id, :name, {:prompt =\> true} %\>

  

\<%= link\_to\_function “- Remove Market”, “$(this).up  
(‘.schedule’).remove()” %\>

  
\<%= schedule\_form.check\_box :monday %\>  
\<%= schedule\_form.check\_box :tuesday %\>  
\<%= schedule\_form.check\_box :wednesday %\>  
\<%= schedule\_form.check\_box :thursday %\>  
\<%= schedule\_form.check\_box :friday %\>  
\<%= schedule\_form.check\_box :saturday %\>  
\<%= schedule\_form.check\_box :sunday %\>  
\<% end -%\>

User Model:

validates\_associated :schedules, :on =\> :update  
after\_update :save\_schedules

accepts\_nested\_attributes\_for :schedules, :allow\_destroy =\> :true,  
:reject\_if =\> :all\_blank

def new\_schedule\_attributes=(schedule\_attributes)  
schedule\_attributes.each do |attributes|  
schedules.build(attributes)  
end  
end

def existing\_schedule\_attributes=(schedule\_attributes)  
schedules.reject(&:new\_record?).each do |schedule|  
attributes = schedule\_attributes[schedule.id.to\_s]  
if attributes  
schedule.attributes = attributes  
else  
schedules.delete(schedule)  
end  
end  
end

def save\_schedules  
schedules.each do |schedule|  
schedule.save(false)  
end  
end

User Controller

def new  
@user = User.new  
end

def create  
cookies.delete :auth\_token  
@user = User.new(params[:user])  
@user.save!  
flash[:notice] = “Thanks for signing up! Please check your email to  
activate your account before logging in.”  
redirect\_to login\_path  
rescue ActiveRecord::RecordInvalid  
flash[:error] = “There was a problem creating your account.”  
render :action =\> ‘new’  
end

def edit  
@user = current\_user  
end

def update  
params[:user][:existing\_schedule\_attributes] ||= {}  
params[:user][:existing\_season\_attributes] ||= {}

@user = User.find(current\_user)  
if @user.update\_attributes(params[:user])  
flash[:notice] = “Your information has been updated.”  
redirect\_to :action =\> ‘show’, :id =\> current\_user  
else  
render :action =\> ‘edit’  
end  
end

def destroy  
@user = User.find(params[:id])  
if @user.update\_attribute(:enabled, false)  
flash[:notice] = “User disabled”  
else  
flash[:error] = “There was a problem disabling this user.”  
end  
redirect\_to :back #action =\> ‘index’  
end

Helpers: users\_helper.rb

module UsersHelper  
def add\_schedule\_link(name)  
link\_to\_function name do |page|  
page.insert\_html :bottom, :schedules, :partial =\>  
‘schedule’, :object =\> Schedule.new  
end  
end

end

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [October 6, 2009, 10:16am UTC](https://www.ruby-forum.com/t/problems-processing-multiple-form-elements-generated-by-javascript-actions/176794/2 "2009-10-06T10:16:41Z")

</div>

As a quick follow up, it looks like the first problem I’m facing is  
because of the checkboxes. Ryan actually states at the end of his  
chapter in Rails Recipes that checkboxes won’t work because their  
value is not passed by the browser when the box is unchecked. So you  
cannot tell which market a given checkbox belongs to when a new  
schedule record is created. His solution is to use a select menu for  
boolean fields, where No =false and Yes = true. I tried that and it  
does fix my problem. However, I’d prefer to use checkboxes if there’s  
a way around this.

My second problem regarding destroying schedule records still remains  
outstanding.

Thanks,  
Mike
