Created
November 21, 2008 23:05
-
-
Save gerhard/27683 to your computer and use it in GitHub Desktop.
This file contains 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
def create | |
@company = Company.new(params[:company]) | |
begin | |
if @company.save | |
redirect_to_index("Company was successfully created") | |
else | |
render :action => "new" | |
end | |
rescue ActiveRecord::RecordInvalid | |
render :action => "new" | |
end | |
end |
This file contains 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
<% fields_for_model(:parent_model => @company, :child_model => address) do |address_form| %> | |
<div class="address clear" id="<%= address.object_id -%>"> | |
<% @address = address %> | |
<%= error_messages_for :address %> | |
<div class="column span-8 first"> | |
<p> | |
<%= address_form.label :address1 -%> | |
<%= address_form.text_field :address1 -%> | |
</p> | |
<p> | |
<%= address_form.label :address2 -%> | |
<%= address_form.text_field :address2 -%> | |
</p> | |
</div> | |
<div class="column span-5"> | |
<p> | |
<%= address_form.label :town -%> | |
<%= address_form.text_field :town -%> | |
</p> | |
<p> | |
<%= address_form.label :county -%> | |
<%= address_form.text_field :county -%> | |
</p> | |
</div> | |
<div class="column span-5"> | |
<p> | |
<%= address_form.label :postcode -%> | |
<%= address_form.text_field :postcode -%> | |
</p> | |
<p> | |
<%= address_form.label :main, "Main Address" -%> | |
<%= check_box_tag "company[#{item_prefix(address)}_address_attributes][#{address.id}][main]", "1", check_if_attribute_is_set(:model_to_check => address, :attribute => "main"), {:id => "company_#{item_prefix(address)}_address_attributes_#{address.id}_main" } %> | |
</p> | |
</div> | |
<div class="column span-5 last"> | |
<label> </label> | |
<p><%= link_to_function "<img src=\"/stylesheets/plugins/buttons/icons/delete.png\" alt=\"Delete icon\"/> Delete address", "destroy(this)", :class => "button negative" %></p> | |
</div> | |
</div> | |
<% end %> |
This file contains 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 Address < ActiveRecord::Base | |
has_many :address_linkings | |
validates_presence_of :address1 | |
validates_uniqueness_of :postcode, :if => Proc.new { |address| !address.postcode.blank? } | |
end |
This file contains 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 AddressLinking < ActiveRecord::Base | |
belongs_to :address | |
belongs_to :address_linkable, :polymorphic => true | |
end |
This file contains 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 Company < ActiveRecord::Base | |
has_many :address_linkings, :as => :address_linkable | |
has_many :addresses, :through => :address_linkings | |
after_update :save_addresses | |
validates_associated :addresses | |
def new_address_attributes=(address_attributes) | |
address_attributes.each do |attributes| | |
addresses.build(attributes) | |
end | |
end | |
def existing_address_attributes=(address_attributes) | |
addresses.reject(&:new_record?).each do |address| | |
attributes = address_attributes[address.id.to_s] | |
if attributes | |
address.attributes = attributes | |
address.main = "0" if attributes[:main].blank? | |
else | |
addresses.delete(address) | |
end | |
end | |
end | |
def save_addresses | |
addresses.each do |address| | |
address.save(false) # false so that it doesn't go through validation (we want to do that before) | |
end | |
end | |
end |
This file contains 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 Admin::CompanyController < ApplicationController | |
def new | |
@company = Company.new | |
@company.addresses.build | |
end | |
def create | |
@company = Company.new(params[:company]) | |
if @company.save | |
redirect_to_index("Company was successfully created") | |
else | |
render :action => "new" | |
end | |
end | |
end |
This file contains 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
ActiveRecord::RecordInvalid in Admin/companyController#create | |
Validation failed: Postcode has already been taken | |
vendor/rails/activerecord/lib/active_record/validations.rb:1021:in `save_without_dirty!' | |
vendor/rails/activerecord/lib/active_record/dirty.rb:87:in `save_without_transactions!' | |
vendor/rails/activerecord/lib/active_record/transactions.rb:150:in `save!' | |
vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:66:in `transaction' | |
vendor/rails/activerecord/lib/active_record/transactions.rb:129:in `transaction' | |
vendor/rails/activerecord/lib/active_record/transactions.rb:138:in `transaction' | |
vendor/rails/activerecord/lib/active_record/transactions.rb:150:in `save!' | |
vendor/rails/activerecord/lib/active_record/transactions.rb:158:in `rollback_active_record_state!' | |
vendor/rails/activerecord/lib/active_record/transactions.rb:150:in `save!' | |
vendor/rails/activerecord/lib/active_record/associations/has_many_through_association.rb:53:in `insert_record' | |
vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:145:in `send' | |
vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:145:in `send' | |
vendor/rails/activerecord/lib/active_record/associations.rb:1391:in `after_create_or_update_associated_records_for_addresses' | |
vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:359:in `method_missing_without_paginate' | |
vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:212:in `method_missing' | |
vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:212:in `each' | |
vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:212:in `send' | |
vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:212:in `method_missing' | |
vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:359:in `method_missing_without_paginate' | |
/opt/local/lib/ruby/gems/1.8/gems/mislav-will_paginate-2.3.6/lib/will_paginate/finder.rb:167:in `method_missing' | |
vendor/rails/activerecord/lib/active_record/associations.rb:1391:in `after_create_or_update_associated_records_for_addresses' | |
vendor/rails/activesupport/lib/active_support/callbacks.rb:178:in `send' | |
vendor/rails/activesupport/lib/active_support/callbacks.rb:178:in `evaluate_method' | |
vendor/rails/activesupport/lib/active_support/callbacks.rb:166:in `call' | |
vendor/rails/activesupport/lib/active_support/callbacks.rb:93:in `run' | |
vendor/rails/activesupport/lib/active_support/callbacks.rb:92:in `each' | |
vendor/rails/activesupport/lib/active_support/callbacks.rb:92:in `send' | |
vendor/rails/activesupport/lib/active_support/callbacks.rb:92:in `run' | |
vendor/rails/activesupport/lib/active_support/callbacks.rb:277:in `run_callbacks' | |
vendor/rails/activerecord/lib/active_record/callbacks.rb:315:in `callback' | |
vendor/rails/activerecord/lib/active_record/callbacks.rb:238:in `create_without_timestamps' | |
vendor/rails/activerecord/lib/active_record/timestamp.rb:29:in `create' | |
vendor/rails/activerecord/lib/active_record/base.rb:2691:in `create_or_update_without_callbacks' | |
vendor/rails/activerecord/lib/active_record/callbacks.rb:222:in `create_or_update' | |
vendor/rails/activerecord/lib/active_record/base.rb:2375:in `save_without_validation' | |
vendor/rails/activerecord/lib/active_record/validations.rb:1009:in `save_without_dirty' | |
vendor/rails/activerecord/lib/active_record/dirty.rb:79:in `save_without_transactions' | |
vendor/rails/activerecord/lib/active_record/transactions.rb:179:in `send' | |
vendor/rails/activerecord/lib/active_record/transactions.rb:179:in `with_transaction_returning_status' | |
vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:66:in `transaction' | |
vendor/rails/activerecord/lib/active_record/transactions.rb:129:in `transaction' | |
vendor/rails/activerecord/lib/active_record/transactions.rb:138:in `transaction' | |
vendor/rails/activerecord/lib/active_record/transactions.rb:178:in `with_transaction_returning_status' | |
vendor/rails/activerecord/lib/active_record/transactions.rb:146:in `save' | |
vendor/rails/activerecord/lib/active_record/transactions.rb:158:in `rollback_active_record_state!' | |
vendor/rails/activerecord/lib/active_record/transactions.rb:146:in `save' | |
app/controllers/admin/company_controller.rb:26:in `create' | |
PARAMS | |
{"company"=>{"name"=>"New company", | |
"new_address_attributes"=>[{"address1"=>"Address 1", | |
"address2"=>"Address 2", | |
"postcode"=>"A1 2BC", | |
"county"=>"County", | |
"town"=>"Town"}, | |
{"address1"=>"Another Address 1", | |
"address2"=>"Another Address 2", | |
"postcode"=>"A1 2BC", | |
"county"=>"Another County", | |
"town"=>"Another Town"}], | |
"archived"=>"0", | |
"fax"=>"Fax", | |
"telephone"=>"Telephone"}, | |
"commit"=>"Create Client"} |
This file contains 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
def item_prefix(item) | |
item.new_record? ? 'new' : 'existing' | |
end | |
def fields_for_model(options = {}, &block) | |
parent_model = options[:parent_model] | |
child_model = options[:child_model] | |
fields_for("#{parent_model.class.name.downcase}[#{item_prefix(child_model)}_#{child_model.class.name.downcase}_attributes][]", child_model, &block) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment