Skip to content

Instantly share code, notes, and snippets.

@akinsgre
Created December 8, 2011 14:29
Show Gist options
  • Save akinsgre/1447135 to your computer and use it in GitHub Desktop.
Save akinsgre/1447135 to your computer and use it in GitHub Desktop.
Problems with Rails form_for tag
#This form "should" create a new contact and a new group_contact record. What I really
# want to do is to add an existing contact to a group. But can't figure out
# how to build a form_for that will let me do that.
#
# If I were creating my own form, I'd just put the group_id in a hidden field and let
# the user pick a contact from a select. Then I'd post to a groups_controller
# to add the contact to group.contacts.
#
# I'm just not seeing how to do that in a form_for
#
# form_for tag usage
<%= form_for [@groups[0], Contact.new], :remote => true do |f| %>
<%= f.label :name %>
<%= f.select :name, options_for_select([['SMS', 1],['Phone', 2]]) %>
<%= submit_tag('Save', :class => "button") %>
<% end %>
# html
<form accept-charset="UTF-8" action="/groups/1/contacts" class="new_contact" data-remote="true" id="new_contact" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="w26+P/qrlh7r227koUYWzkypLmXmIGGKLJelhQ1WDzw=" /></div>
<label for="contact_name">Name</label>
<select id="contact_name" name="contact[name]"><option value="1">SMS</option>
<option value="2">Phone</option></select>
<input class="button" name="commit" type="submit" value="Save" />
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment