Add and remove try but missing concepts

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

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 %>
        <%= collection_select(:coffee,:foos,
            @camp_campers, :id, :full_name,
            {}, html =>{ :size => 20 }) %> <br />
        <%= submit_tag "Add to Team", :name => 'add_to' %>
    <%  end %>

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

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

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)
if params[:add_to]
acamper_id = params[:coffee][:foos]
acamper = Camper.find_by_id(acamper_id)
@team.campers << acamper
session[:team]= @team
redirect_to :action => ‘teams_campers’

(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' %>

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])
@team = session[:team] || @camp_teams[0]
if ! @team
redirect_to :action => ‘create_team’
@team_campers = @team.campers || nil
@coach_players = @coach.campers || nil

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.

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,
{ :size => 20 }) %>

<%= submit_tag “Remove Camper”, :name => ‘remove_from’
        <%= select_tag(:camper_to_add,
           options_from_collection_for_select( @coach_campers, :id, 

{ :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

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

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


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 } )%>


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