profiles_controller.rb 2.46 KB
Newer Older
1 2
class ProfilesController < ApplicationController
  before_action :set_profile, only: [:show, :edit, :update, :destroy]
3
  before_action :set_carmen_locale, only: [:show, :edit, :new, :states]
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

  # GET /profiles
  # GET /profiles.json
  def index
    @profiles = Profile.all
  end

  # GET /profiles/1
  # GET /profiles/1.json
  def show
  end

  # GET /profiles/new
  def new
    @profile = Profile.new
  end

  # GET /profiles/1/edit
  def edit
  end

  # POST /profiles
  # POST /profiles.json
  def create
    @profile = Profile.new(profile_params)
29
    @profile.user_id = current_user.id
30 31 32

    respond_to do |format|
      if @profile.save
33
        format.html { redirect_to @profile, notice: I18n.t('successfully_created_profile') }
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
        format.json { render :show, status: :created, location: @profile }
      else
        format.html { render :new }
        format.json { render json: @profile.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /profiles/1
  # PATCH/PUT /profiles/1.json
  def update
    respond_to do |format|
      if @profile.update(profile_params)
        format.html { redirect_to @profile, notice: 'Profile was successfully updated.' }
        format.json { render :show, status: :ok, location: @profile }
      else
        format.html { render :edit }
        format.json { render json: @profile.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /profiles/1
  # DELETE /profiles/1.json
  def destroy
    @profile.destroy
    respond_to do |format|
      format.html { redirect_to profiles_url, notice: 'Profile was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

66 67 68 69 70
  def states
    states = Carmen::Country.named(states_params).subregions
    render partial: 'profiles/states', locals: { states: states }
  end

71 72 73 74 75 76 77 78 79 80
  private
    # Use callbacks to share common setup or constraints between actions.
    def set_profile
      @profile = Profile.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def profile_params
      params.require(:profile).permit(:first_name, :last_name, :birthdate, :gender, :state, :city, :country, :school, :responsible_document, :responsible_name, :receive_mass_email, :phone, :user_id, :address, :zip_code, :occupation, :school_type)
    end
81 82 83 84

    def set_carmen_locale
      Carmen.i18n_backend.locale = I18n.locale.to_s.split('-').first
    end
85 86 87 88

    def states_params
      params.require(:country)
    end
89
end