Skip to content

Instantly share code, notes, and snippets.

@arjan0307
Created March 20, 2012 15:29
Show Gist options
  • Save arjan0307/2137051 to your computer and use it in GitHub Desktop.
Save arjan0307/2137051 to your computer and use it in GitHub Desktop.
Nested forms
class Conversation < ActiveRecord::Base
has_many :messages
has_many :participants
has_many :users, :through => :participants
accepts_nested_attributes_for :messages
end
class ConversationsController < ApplicationController
load_and_authorize_resource :through => :current_user
respond_to :html, :json, :js
def index
end
def new
@conversation.messages.build
end
def create
@conversation.save
respond_with @conversation
end
end
class Message < ActiveRecord::Base
belongs_to :conversation
belongs_to :author, :class_name => 'User'
validates_presence_of :conversation
validates_presence_of :author
validates_presence_of :content
end
<script type="text/javascript">
$(function() {
$(".chzn-select").chosen(); $(".chzn-select-deselect").chosen({allow_single_deselect:true});
});
</script>
<%= @conversation.errors.messages %>
<%= form_for @conversation, :validate => true do |f| %>
<div><%= f.label :user_ids, 'Recipients' %></div>
<div><%= f.collection_select(:user_ids, User.all, :id, :email, options ={}, :class =>"chzn-select", :multiple => 'multiple') %></div>
<%= f.fields_for :messages do |fm| %>
<div><%= fm.label :content %></div>
<div><%= fm.text_area :content %></div>
<% end %>
<div><%= f.submit %></div>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment