/app/controllers/contacts_controller.rb
Ruby | 77 lines | 59 code | 14 blank | 4 comment | 2 complexity | 867177568fa6bae4b8ed4dd3cebe502a MD5 | raw file
- class ContactsController < ApplicationController
- before_action :set_contact, only: [:show, :edit, :update, :destroy, :share]
- skip_before_action :verify_authenticity_token, :only => [:import]
- def share
- # raise
- @contact.share(params[:email])
- end
- def import
- Contact.import(params[:file])
- redirect_to contacts_path
- end
- def index
- # @contacts = Contact.all
-
- @q = Contact.ransack(params[:q])
- @contacts = @q.result(distinct: true)
- respond_to do |format|
- format.html
- # format.csv { render text: @contacts.to_csv }
- format.csv { send_data @contacts.to_csv }
- end
- end
- def show
- end
- def new
- @contact = Contact.new
- end
- def edit
- end
- def create
- @contact = Contact.new(contact_params)
- # raise
- respond_to do |format|
- if @contact.save
- format.html { redirect_to @contact }
- else
- format.html { render :new }
- end
- end
- end
- def update
- respond_to do |format|
- if @contact.update(contact_params)
- format.html { redirect_to @contact }
- else
- format.html { render :edit }
- end
- end
- end
- def destroy
- @contact.destroy
- respond_to do |format|
- format.html { redirect_to contacts_url }
- format.js
- end
- end
- private
- def set_contact
- @contact = Contact.find(params[:id])
- end
- def contact_params
- params.require(:contact).permit(:first_name, :last_name, :email_string, :phone_string)
- end
- end