# Removing an element from an array

**URL:** <https://www.ruby-forum.com/t/removing-an-element-from-an-array/133003>\
**Category:** Rails\
**Created:** [March 26, 2008, 5:20pm UTC](https://www.ruby-forum.com/t/removing-an-element-from-an-array/133003 "2008-03-26T17:20:16Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![RoaR\_lion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3ec8ea/32.png) [@RoaR\_lion](https://www.ruby-forum.com/u/RoaR_lion)\
**Post date:** [March 26, 2008, 5:20pm UTC](https://www.ruby-forum.com/t/removing-an-element-from-an-array/133003/1 "2008-03-26T17:20:16Z")

</div>

Hey,

I’m adding objects to an array, where they are displayed elsewhere on  
the page.  
I went to add the remove method, called by the remove button on the  
page, and it’s not functioning as I would hope. I figured this is a  
straightforward thing, but I want to do it the right way.

Add in view  
\<%= link\_to\_remote ‘Add’,  
:url =\> { :action =\> ‘add\_subject’, :id =\> subject },  
:update =\> ‘added\_subjects’%\>

Add in controller  
def add\_subject  
@added\_subjects = find\_added\_subjects  
@added\_subjects \<\< Subject.find(params[:id])

```
render :partial => 'added_subjects', :layout => false

```

end

I tried this for the remove,

View  
\<%= link\_to\_remote ‘Remove’,  
:url =\> { :action =\> ‘remove\_subject’, :id =\> subject },  
:update =\> ‘added\_subjects’ %\>

Controller  
def remove\_subject  
@added\_subjects = find\_added\_subjects  
@added\_subjects.reject! { |subject| subject.id == params[:id] }

```
render :partial => 'added_subjects', :layout => false

```

end

Thanks!
