Created
March 20, 2012 15:29
-
-
Save arjan0307/2137051 to your computer and use it in GitHub Desktop.
Nested forms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Conversation < ActiveRecord::Base | |
has_many :messages | |
has_many :participants | |
has_many :users, :through => :participants | |
accepts_nested_attributes_for :messages | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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