Last active
August 29, 2015 14:16
-
-
Save luckyruby/43a0c0c47b19257d3424 to your computer and use it in GitHub Desktop.
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
.modal-dialog | |
.modal-content | |
.modal-header | |
%button.close{type: 'button', 'aria-hidden' => "true", 'data-dismiss' => 'modal'} × | |
%h4.modal-title Licensee | |
.modal-body | |
= form_for @licensee, html: { autocomplete: 'off', class: 'form-horizontal' }, remote: true do |f| | |
= render 'shared/errors', object: @licensee | |
.control-group | |
%label.control-label Name | |
.controls.controls-row= f.text_field :name, class: 'input-xlarge' | |
.control-group | |
%label.control-label Locations | |
.controls.controls-row= f.collection_select :location_ids, @locations, :id, :code, {include_hidden: false}, multiple: true | |
.form-actions | |
= f.submit 'SAVE', class: 'btn btn-primary' |
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
ActiveRecord::RecordInvalid (Validation failed: Location has already been taken): | |
app/controllers/licensees_controller.rb:41:in `block (2 levels) in update' | |
app/controllers/licensees_controller.rb:38:in `update' |
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 Licensee < ActiveRecord::Base | |
has_many :licensee_locations, dependent: :destroy, autosave: true | |
has_many :locations, through: :licensee_locations | |
validates :name, presence: true | |
validates :name, uniqueness: true | |
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 LicenseeLocation < ActiveRecord::Base | |
belongs_to :licensee | |
belongs_to :location | |
validates :licensee, :location, presence: true | |
validates :location, uniqueness: true | |
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 LicenseesController < ApplicationController | |
before_action :authenticate_user! | |
before_action { authorize! } | |
before_action { css_select 'admin' } | |
def index | |
load_licensees | |
end | |
def new | |
respond_to do |format| | |
format.js { | |
@licensee = Licensee.new | |
render_form | |
} | |
end | |
end | |
def create | |
respond_to do |format| | |
format.js { | |
@licensee = Licensee.new(safe_params) | |
@licensee.save ? render_list : render_form | |
} | |
end | |
end | |
def edit | |
respond_to do |format| | |
format.js { | |
find_licensee | |
render_form | |
} | |
end | |
end | |
def update | |
respond_to do |format| | |
format.js { | |
find_licensee | |
@licensee.update(safe_params) ? render_list : render_form | |
} | |
end | |
end | |
def destroy | |
respond_to do |format| | |
format.js { | |
find_licensee | |
@licensee.destroy | |
render_list | |
} | |
end | |
end | |
private | |
def load_licensees | |
@licensees = Licensee.order(:name) | |
end | |
def find_licensee | |
@licensee = Licensee.find(params[:id]) | |
end | |
def load_locations | |
@locations = Location.order(:code) | |
end | |
def render_list | |
load_licensees | |
render template: 'licensees/list' | |
end | |
def render_form | |
load_locations | |
render template: 'licensees/form' | |
end | |
def safe_params | |
params.require(:licensee).permit(:name, location_ids: []) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment