Skip to content

Instantly share code, notes, and snippets.

@ricardodovalle
Created October 23, 2014 22:14
Show Gist options
  • Save ricardodovalle/41265eeedd6fa5887be9 to your computer and use it in GitHub Desktop.
Save ricardodovalle/41265eeedd6fa5887be9 to your computer and use it in GitHub Desktop.
= simple_form_for person, remote: true do |f|
.row
.col-xs-12.col-sm-12.col-md-6
= f.fields_for :address do |builder|
= render 'form_address', f: builder
.row
.col-xs-12.col-sm-12.col-md-12
%strong
%small= t 'activemodel.models.address.one'
.row
.col-xs-6.col-sm-4.col-md-4
= f.input :zip, input_html: {class: 'uppercase'}
.col-xs-12.col-sm-2.col-md-2
= f.input :state_abbr, input_html: {class: 'uppercase fetch-me'}, label: 'UF'
.col-xs-12.col-sm-6.col-md-6
= f.input :city, input_html: {class: 'uppercase fetch-me'}
.row
.col-xs-12.col-sm-4.col-md-4
= f.input :province, input_html: {class: 'uppercase fetch-me'}
.col-xs-12.col-sm-8.col-md-8
= f.input :street, input_html: {class: 'uppercase fetch-me'}
.row
.col-xs-8.col-sm-8.col-md-8
= f.input :compl, label: 'Compl', input_html: {class: 'uppercase fetch-me'}
.col-xs-4.col-sm-4.col-md-4
= f.input :number, label: 'Number', input_html: {class: 'fetch-me'}
class PeopleController < ApplicationController
respond_to :html, :js, :json
responders :flash, :http_cache
def new
@person = Person.new
authorize @person
@person.build_address
end
def edit
@person = get_person(params[:id])
authorize @person
end
def create
@person = Person.new(person_params)
authorize @person
if @person.save
else
respond_with(@person.errors, status: :unprocessable_entity)
end
end
def update
@person = get_person(params[:id])
authorize @person
if @person.update(person_params)
else
respond_with(@person.errors, status: :unprocessable_entity)
end
end
private
def get_persons
Person.all.decorate
end
def get_person(params_id)
Person.find(params_id).decorate
end
def person_params
params.require(:person).permit(*policy(@person || Person).permitted_attributes)
end
end
class PersonPolicy < ApplicationPolicy
def permitted_attributes
if @user.has_role?(:thorx) || @user.has_role?(:admin)
[:cpf, :name, :rg, :birth, :birth_text, :email, :phone, {card_ids: []},
:cnh, {category_ids: []}, :original_updated_at,
{address_attributes: [:id, :zip, :city, :province, :state, :state_abbr,
:nation, :street, :compl, :number, :person_id]}
]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment