Posts Ruby on Rails: api collection helpers html select
by bseanvt
leave a comment
Collection Select Helper and OnChange Event in Rails
Given a collection of Active Record objects, you may use the collection_select helper method to produce a select form field. You need to pass in a number of arguments to the helper function.
1) object – your model object used in the collection
2) method – a valid model attribute or method
3) collection – a collection of active record model objects
4) option_value – value being set from the model for the <option value=”option_value”> html element
5) option_name – what is displayed for the user e.g., <option> option_name <
6) option – general options
7) options for the select html element
# helper and arguments...
# collection_select( model, id, collection, option_value, option_name, options, html_options)
<%= collection_select("states", "state_id",
State.participating,
"abbreviation", "name",
{:selected=> get_current_state_or_nil },
{:onchange=>"document.location='/states/'+this.value"}
) %>
#which will produce something like...
<pre id="line27"><select id="state_id" name="state[id]" onchange="document.location='/states/'+this.value">
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
</select>
NO Table Cell Spacing Please
Remove cell padding on a table you add the cellspacing attribute and set it to “0″. Is there a better way to do this with straight up CSS? Nothing seems to work…
<table id='my-favorite-table' cellspacing="0" > ...
Programming: block concat content_tag dry helpers html strings views wrapper yield
by bseanvt
2 comments
Yield a Block within Rails Helper Method with Multiple Content_tags Using Concat
To clean up some repetitive html coding in views, pass a block of text to a helper function which will wrap it for you the same way, each and every time. For example, I have a ‘help’ link which will toggle the display of a block of text if a link is clicked. I use this “+/- help” link throughout my application in various views. I could create a partial but this gets messy. Instead this is what I want to write in my views…
<% help do %> here is my help text... <% end %>
which will render HTML similar to this…
<a href='#' 'class='help' id='help_link_123456' onclick='some_func_to_toggle_state'>+/- help</a> <div id="help_123456"> here is my help text... </div>
In order to accomplish this you need to create a helper method. Assigning a unique id, just a random number, will help avoid collisions with the state of the toggled div if you use this more than once per page. Placing other HTML helper methods inside the concat() method allows multiple tags as well as rendering the block passed to the function in the appropriate place. It looks a little unwieldy, but works nicely.
# app/helpers/application_helper.rb
module ApplicationHelper
def help(&block)
uniqid = rand; concat( link_to_function("+/- help") do |page|
page["help_#{uniqid}"].toggle
page.visual_effect :highlight, "help_#{uniqid}"
end + content_tag(:div,:class=>"help",:id=>"help_#{uniqid}", :style=>"display:none") do
yield
end )
end
end


