about me

I'm a 37'ish year old web application developer from South Portland, Maine. I love meeting fellow techies, drop me a line if you want to talk shop.

Categories

Form builders in Rails: field names and ids for Javascript

Posted on 09/27/2010 at 05:09 PM

I came up with a few convenience methods today which will help you discover what Rail’s generated NAME and ID attributes will be for a specific field on a given form builder object. Actually, I didn’t come up with them so much as extract them from the built in form builder. It was useful enough to me that I thought I might share my discovery. I can’t tell you how many times I’ve lazily hardcoded an ID onto a form element just to bypass the defaults.

Throw these 2 methods in your ApplicationHelper module:

def field_id_for_js(builderattribute)
    
"#{builder.object_name}[#{attribute.to_s.sub(/\?$/,"")}]".gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "")
end

def field_name_for_js
(builderattribute)
    
"#{builder.object_name}[#{attribute.to_s.sub(/\?$/,"")}]"
end

Here is an example using Rails nested attribute forms:

<%= form_for(@submission) do |f| %>
    <%= 
f.fields_for :project, @submission.project do |builder| %>

        <
div class="field">
            <%= 
builder.label :classified"Classified?" %><br/>
            <%= 
builder.radio_button :classifiedfalse %> <%= builder.label :classified_false"No" %>
            <%= 
builder.radio_button :classifiedtrue %>  <%= builder.label :classified_true"Yes" %>
        </
div>

        <%= 
content_for(:head) do %>
            <
script>
                $(function() 
{
                    
// get our value by name
                        
$("input[name='<%= field_name_for_js(builder, :classified) %>']").val();
                    
// set our value by ID
                        
$("#<%= field_id_for_js(builder, :classified_true) %>").attr('checked'true);
                
});
            
</script>
        
<% end %>

    <% 
end %>
<% 
end %>

Would output:

<script>
$(function() 
{
        
// get our value by name
            
$("input[name='submission[project_attributes][classified]']").val();
        
// set our value by ID
            
$("#submission_project_attributes_classified_true").attr('checked'true);
});
</script>

In case you missed it ...

<%= field_name_for_js(builder, :classified) %>
<%= 
field_id_for_js(builder, :classified_true) %>

... produced ...

submission[project_attributes][classified]
submission_project_attributes_classified_true


Pretty handy stuff smile

1 Comments

Comment #1 by madhavi  on  07/20  at  06:27 PM

’<%= f.fields_for rasberryroject, @submission.project do |builder| %>’

What is ‘project’ here?
Could you please explain?

Thanks!

Leave a comment?

Please use Pastie or Gist if you need to write code in your comments.

Name:

Email:

Location:

URL:

Remember my personal information

Notify me of follow-up comments?

Please enter the word you see in the image below:


RailsConf 2008 Often times I will release code for free or go that extra distance to help others online. If my skills were useful to you, please consider a small donation. Thank you very much.

recommend me!

Search

You Can Find Me

@ github.com
@ twitter.com
@ calendaraboutnothing

My Wishlists

@ Amazon.com

My Other Sites

Foundation's Edge, RJones Family, We're Not.com (only for staging), Ailee Jones (same as rjones for now)

Friends of Mine

Aaron, Barnaby, Brian, Chris, Dirk, Frank, Four, Justin, Matt, Mike, Monty, Paul, Sean, Travis

IRC Hangouts

I can usually be found lounging on irc.freenode.net while I work, on the following channels: #fauna, #github, #hello-heroku, #jquery, #passenger, #ruby, #rubyonrails, #slicehost, #sproutcore, #textmate, #werenot.