# Habtm user interface; unexpected behaviour (calling update instead of method?)

**URL:** <https://www.ruby-forum.com/t/habtm-user-interface-unexpected-behaviour-calling-update-instead-of-method/124639>\
**Category:** Rails\
**Created:** [December 31, 2007, 12:42pm UTC](https://www.ruby-forum.com/t/habtm-user-interface-unexpected-behaviour-calling-update-instead-of-method/124639 "2007-12-31T12:42:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaeptnhaddock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/d26b3c/32.png) [@kaeptnhaddock](https://www.ruby-forum.com/u/kaeptnhaddock)\
**Post date:** [December 31, 2007, 12:42pm UTC](https://www.ruby-forum.com/t/habtm-user-interface-unexpected-behaviour-calling-update-instead-of-method/124639/1 "2007-12-31T12:42:03Z")

</div>

I’m trying to get a user-interface going, where the user can choose  
which users he/she would like to add to a group. edit.html.erb for the  
Group displays a list of users currently assigned to the group we’re  
editing, as well as a list of users not assigned to this group yet. On  
both lists there are buttons to assign a user to the group resp. to  
remove the user from the group. (see code below).

This works. Almost. There are some unexpected behaviours that puzzle  
me:

- the topmost row on both lists cannot be moved to the other list.  
When clicking the ‘\>’ or ‘\<’ button next to the topmost-listelement  
the application executes an “update” command instead of adding or  
removing the user via the action =\> command that is assigned to the  
button. (when I sort the users according to id intead of names, it is  
still the top-row that can’t be moved with the buttons; it must have  
something to do with the position, I think)

- the update-button as well as the “show” and “back” links are being  
displayed at the very top, even though, in the code they follow after  
the lists… an indication that I’m doing something fundementally  
wrong, it seems to me.

can you get me back on track? Any comments and improvements are very  
welcome!  
thank you for your time  
haddock

ps I’m planning to use partials later on, but for the moment I would  
like to get this working first

---------------------------------------------------------views/groups/  
edit.html.erb

# Editing group

\<%= error\_messages\_for :group %\>

\<% form\_for(@group) do |f| %\>

**Name**  
 \<%= f.text\_field :name %\>

\<% for user in @users\_of\_group %\>

  
  
  
  
  
\<% end %\>

| Users of this group | |
| --- | --- |
| \<%= button\_to ‘\>’, :action  
=\> :remove\_user\_from\_group, :user\_id =\> user.id, :group\_id =\>  
@group.id %\> | \<%=h user.id %\> | \<%=h user.name %\> |

\<% for user in @users\_not\_of\_group %\>

  
  
  
  
  
\<% end %\>  

\<%= f.submit "Update" %\>

\<% end %\>

\<%= link\_to ‘Show’, @group %\> |  
\<%= link\_to ‘Back’, groups\_path %\>

---------------------------------------------------------excerpt from  
groups\_controller.rb

# GET /groups/1/edit

def edit  
@group = Group.find(params[:id])  
@users\_of\_group = @group.users.find(:all, :order =\> “name”)  
@users\_not\_of\_group = User.find\_by\_sql([“select \* from users where  
id not in (select user\_id from groups\_users where group\_id = ?) order  
by name”, params[:id]])  
end

def add\_user\_to\_group  
@user = User.find(params[:user\_id])  
@group = Group.find(params[:group\_id])  
@group.users \<\< @user  
redirect\_to :action =\> :index  
end

def remove\_user\_from\_group  
@user = User.find(params[:user\_id])  
@group = Group.find(params[:group\_id])  
@group.users.delete(@user)  
redirect\_to :action =\> :index  
end

| Users available | |
| --- | --- |
| \<%= button\_to ‘\<’, :action =\> :add\_user\_to\_group, :user\_id  
=\> user.id, :group\_id =\> @group.id %\> | \<%=h user.id %\> | \<%=h user.name %\> |

---

<div class="post-metadata">

**Author:** ![kaeptnhaddock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/d26b3c/32.png) [@kaeptnhaddock](https://www.ruby-forum.com/u/kaeptnhaddock)\
**Post date:** [December 31, 2007, 9:48pm UTC](https://www.ruby-forum.com/t/habtm-user-interface-unexpected-behaviour-calling-update-instead-of-method/124639/2 "2007-12-31T21:48:11Z")

</div>

On 12/31/07, kaeptnhaddock [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> the lists… an indication that I’m doing something fundementally  
> wrong, it seems to me.

I had a similar problem where things were working fine in everything  
but IE. It was solved by adding tags into my tables. Not  
sure if that’s your problem but I thought I’d mention it.

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![kaeptnhaddock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/d26b3c/32.png) [@kaeptnhaddock](https://www.ruby-forum.com/u/kaeptnhaddock)\
**Post date:** [January 6, 2008, 12:33pm UTC](https://www.ruby-forum.com/t/habtm-user-interface-unexpected-behaviour-calling-update-instead-of-method/124639/3 "2008-01-06T12:33:34Z")

</div>

solved!

I shouldn’t have put the \<%= button\_to '\> elements within another  
form. Nested forms are no good, it says so on page 476 in the bible  
(agile web development with rails).

cheers

---

<div class="post-metadata">

**Author:** ![kaeptnhaddock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/d26b3c/32.png) [@kaeptnhaddock](https://www.ruby-forum.com/u/kaeptnhaddock)\
**Post date:** [January 2, 2008, 9:10pm UTC](https://www.ruby-forum.com/t/habtm-user-interface-unexpected-behaviour-calling-update-instead-of-method/124639/4 "2008-01-02T21:10:19Z")

</div>

Thanks for your comment. I did as you told plus I’ve cleaned the html  
a bit, there were some errors as well. The second point about the  
placement of the button/links is now working as expected!

This still leaves the first issue.The fist element/user still wont  
remove itself from the list, if I press the arrow-button next to it.  
Instead the "update"method gets called. With other users/list-items it  
works just fine. Below the html code for the table, as well as the  
update-method. The update-method is a by-product from the scaffold  
command. Maybe to someone who understands it better than I it is  
obvious, why it is called when the top-button of the following table  
is pressed?

\*\*\*---------- update method in controller  
def update  
@group = Group.find(params[:id])

```
respond_to do |format|
  if @group.update_attributes(params[:group])
    flash[:notice] = 'Group was successfully updated.'
    format.html { redirect_to(@group) }
    format.xml { head :ok }
  else
    format.html { render :action => "edit" }
    format.xml { render :xml => @group.errors, :status

```

=\> :unprocessable\_entity }  
end  
end  
end

\*\*\*----------html-source of table

```
<tr>
  <td><form method="post" action="/groups/remove_user_from_group?

```

group\_id=3&user\_id=3" class=“button-to”\>

  

```
  <td>abcdefghij</td>
</tr>

<tr>
  <td><form method="post" action="/groups/remove_user_from_group?

```

group\_id=3&user\_id=2" class=“button-to”\>

  
  
  

```
<tr>
  <td><form method="post" action="/groups/remove_user_from_group?

```

group\_id=3&user\_id=1" class=“button-to”\>

  
  
  

| Users of this group | |
| --- | --- |
| 3 | 2 | aeruoweuroweur | 1 | chaljdfaf |
