# Add and remove try but missing concepts

**URL:** <https://www.ruby-forum.com/t/add-and-remove-try-but-missing-concepts/120250>\
**Category:** Rails\
**Created:** [November 15, 2007, 5:46am UTC](https://www.ruby-forum.com/t/add-and-remove-try-but-missing-concepts/120250 "2007-11-15T05:46:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [November 15, 2007, 5:46am UTC](https://www.ruby-forum.com/t/add-and-remove-try-but-missing-concepts/120250/1 "2007-11-15T05:46:11Z")

</div>

I am spanking new to programming but managed to read a ruby book, did a  
few onlamp tutorials, read and followed along with the depot project on  
the Agile WebDfR, read and followed along the Oreily up and running  
book.

I am now hacking along trying to program a full featured web/app site.  
Well i dont expect it to be easy but each step is a real struggle as  
even poring over the forums, squinting at the api, looking for examples  
in the books, really is still very hard. There are just too few  
examples on what options are available and how to use them. Options?

A basic form with two lists managing a habt relationship seems pretty  
basic (although I would have laughed at the concept 6 months ago when i  
didn’t know what iterating over an array was.)

From hacking around, not really understanding how the myriad of helpers  
interact, I’ve got the following working. I feel like I’m using  
redundant parts and hacking something a bit.

here’s part of the html (its on a page with a few separate forms which  
build the “state” necessary to attack the relationship.

 \<% form\_tag (:action =\> :add\_remove\_camper) do %\> 

```
       <td>
        <%= collection_select(:coffee,:foos,
            @camp_campers, :id, :full_name,
            {}, html =>{ :size => 20 }) %> <br />
        <%= submit_tag "Add to Team", :name => 'add_to' %>
       </td>
    <% end %>
    </tr>
</table>

```

You’ll notice my metasynatic (what fancy new jargon I have learned!)  
:foo, and also :milk :coffee :moo. It took me 10s of painful hours  
trying to grok what was going on because all the books follow the rails  
idiomatic tradition of using a name similar to a model object you’re  
using BUT, once i discovered they were just holders what was going on  
became more logical ad less incantational magic.

It seems a bit weird that I need to use Two, not just one name to store  
the selected value into a parameter than I can access in a controller

question one: am I being lame with that… can I use a combination of  
select tag and collection\_for\_select and make it work? (I tried and  
tried but couldn’t do it, then resorted to the dual metasynatic and it  
worked)

Question 2… HTML options… I am almost crying in frustration from hours  
of scanning forums etc to get some idea of how I can shove css formating  
in there…do i need to set a class and use the style sheet to make the  
box :size = 10 ?

That size thing worked with another form I made using collection for  
select? Why not with select? (I’d like to grok the difference … it  
might help me understand the structure of how things work). You’ll  
notice I put an extra empty has brackets in there hoping that would fill  
the other options (it didn’t work with or without it).

I would love, to read about all the “options” available in the first  
hash too… like I saw in a forum post that I could put :include\_blank =\>  
true… thats neat… what else can i do? I’ll pay the $100 for a 1200  
page book that detailed the options and fleshier code examples using  
the options in real contexts ( I bought the recipe book but the  
examples are also far too terse to show how you’d mesh stuff in in  
practice in busier pages. Thats Question 3… anyone got a good book or  
maybe a well documented and commented full featured web app that I could  
pore over and learn from (shudder at the effort but I don’t know how  
else)

OK, here’s my controller for the rudimentary Add Remove form (i’ll need  
to make it work with multi select too once I get that size thing ironed  
out, I’ll need to use that incrutible “:names[]” trick I saw somewhere)

def add\_remove\_camper  
@team = session[:team]  
if params[:remove\_from]  
scamper\_id = params[:milk][:moos]  
scamper = Camper.find(scamper\_id)  
@team.players.delete(scamper)  
@team.save  
end  
if params[:add\_to]  
acamper\_id = params[:coffee][:foos]  
acamper = Camper.find\_by\_id(acamper\_id)  
@team.campers \<\< acamper  
@team.save  
end  
session[:team]= @team  
redirect\_to :action =\> ‘teams\_campers’  
end

(the model names above have been changed to protect my delusional get  
rich quick scheme idea)  
The dual button one form thing I learned from one of those videos at the  
rails site. I will need to watch all 100 of them to try to catch more  
clues. Getting validation error messages onto an html page in a  
different model controller based on a parent save is going to be tricky  
and time consuming but I guess thats the beast programming is.

I am not really keen on usig a lot of ajax for core functionality I  
will add it later to refresh elements instead of having whole pages  
reload. I want the site to work for non tech people drinking beer and  
pounding on the mouse clicks and refresh buttons.

Well my questions were above, (how do i get the html options snookered  
in and do I really need two variable holders to pass (via params) a  
selected option to the controller?)

But any other suggestions on how to really learn Rails and its  
possibilities would also be appreciated.

| \<%= collection\_select(:milk, :moos, @team\_campers, :id, :full\_name, {}, {:html =\> { :size =\> 20 }} )%\>  
 \<%= submit\_tag "Remove Camper", :name =\> 'remove\_from' %\> |

---

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [November 15, 2007, 7:02am UTC](https://www.ruby-forum.com/t/add-and-remove-try-but-missing-concepts/120250/2 "2007-11-15T07:02:52Z")

</div>

Oh, I forgot to include the redirect action that sets up the instance  
variables for the page where the form is included. (there are 3 forms on  
the page that set the team and create new campers). The user hits a  
button to modify the campers association with the selected team…goes to  
the add\_remove method i showed above, then gets redirected to the page  
set up method below.

The books really don’t show this chaining of controller methods.(i might  
be doing something wrong or my UI desires are different)

def teams\_campers  
@camp = session[:camp]  
@camp\_teams = []  
@camp\_teams = @camp.teams  
@camper = Camper.new(params[:camper])  
@team = session[:team] || @camp\_teams[0]  
if ! @team  
redirect\_to :action =\> ‘create\_team’  
else  
@team\_campers = @team.campers || nil  
@coach\_players = @coach.campers || nil  
end  
end

As you can see, I haven’t got this wired.(I should use .find and :order  
and other safety redirects if session info comes up blank and needs to  
be reset. (does that happen much?)… really the state should be put in  
controller before filters I guess. No questions here… this was just to  
finish the context of the other questions if it mattered.

---

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [November 15, 2007, 5:55pm UTC](https://www.ruby-forum.com/t/add-and-remove-try-but-missing-concepts/120250/3 "2007-11-15T17:55:05Z")

</div>

Thanks for the quick response. I was able to get the class assigned via  
the html option slash as you described. (it would really be nice if  
someone wrote a couple web tutorials outlining a dozen of the most  
common Options for both the first hash and the Html options hash).

> The text\_are helpers take a :size option, IIRC, collection\_select doesn’t

Would I wrap the whole collection\_select with the text\_helper? (sounds  
dicey, I’ll resort to style-sheets even in the development mode I guess)

> “you could just be  
> using select\_tag and collection\_for\_select/options\_for\_select”

Yes, that was my first approach but it didn’t work.

\<% form\_tag (:action =\> :add\_remove\_camper) do %\>

  
\<%= select\_tag(:camper\_to\_remove,  
options\_from\_collection\_for\_select( @team\_campers, :id,  
:full\_name),  
{ :size =\> 20 }) %\>  
  
\<%= submit\_tag “Remove Camper”, :name =\> ‘remove\_from’  
%\>  

```
       <td>
        <%= select_tag(:camper_to_add,
           options_from_collection_for_select( @coach_campers, :id, 

```

:full\_name),  
{ :size =\> 20 }) %\>   
  
\<%= submit\_tag “Add Camper to Team”, :name =\> ‘add\_to’ %\>

When I do it this way, I get parameters like :id =\> 6 (or whatever  
selected)

I dont get anything(I expected the id number) in  
params[:camper\_to\_remove] . I don’t want to use :id as an identifier  
because I might add additional pieces to the form and might not know  
which of the fields’ id I was talking about.

Also, for some reason, the html size function DOES work with  
options\_from\_colletion\_for\_select (but didnt work for  
collection\_select).

---

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [November 15, 2007, 9:47am UTC](https://www.ruby-forum.com/t/add-and-remove-try-but-missing-concepts/120250/4 "2007-11-15T09:47:22Z")

</div>

On 15 Nov 2007, at 04:46, Tom N. wrote:

> \<% form\_tag (:action =\> :add\_remove\_camper) do %\> 
> 
> | \<%= collection\_select(:milk, :moos, @team\_campers, :id, :full\_name, {}, {:html =\> { :size =\> 20 }} )%\>  
> 
> Half the point of the form helpers that use instance variables is that  
> the first one (:milk) is the name of the instance varaible and the  
> second (:moos) is the name of the attribute you are editing. You’re  
> not editing an existing object (or creating one), so you could just be  
> using select\_tag and collection\_for\_select/options\_for\_select
> 
> > \<%= collection\_select(:milk, :moos,  
> > @team\_campers, :id, :full\_name,  
> > {}, {:class =\> ‘foo’} )
> 
> Should set the class of that item to foo. The text\_are helpers take  
> a :size option, IIRC, collection\_select doesn’t
> 
> Fred
> 
> |

---

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [November 15, 2007, 9:03pm UTC](https://www.ruby-forum.com/t/add-and-remove-try-but-missing-concepts/120250/5 "2007-11-15T21:03:08Z")

</div>

I tried and failed to set the “size” element of the select box via  
CSS(the number of entries tall the select box is). I’ll need to keep  
reading on that but there was suprisingly little web information  
available via search on customizing select boxes (Looks like ordered  
scrollable lists with check items are the way to go desing wise).

It appears that the size option isn’t css, but needs to appear in the  
html select tag itself.

LUCKILY, in frustration, I tried again to include size in the html hash.

\<%= collection\_select(:milk, :moos,  
@team\_campers, :id, :full\_name,  
{}, { :class =\> ‘select\_campers’, :size =\> 20 } )%\>  
  
works.

I must have missed some punctuation in earlier attempts. (and including  
the html =\> didn’t seem to be a good idea.).

I still haven’t discovered my error using the preferred combo of other  
helpers select\_tag, and (?) Perhaps I shouldn’t bet using  
options\_from\_collection\_for\_select , but instead  
collection\_for\_select/options\_for\_select.
