eugene_art

Untitled

Jun 14th, 2018
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.73 KB | None | 0 0
  1. class BasicInformationsController < ApplicationController
  2.   before_action :set_basic_information, only: [:show, :edit, :update]
  3.   before_action :set_filiations, only: [:show, :edit, :update]
  4.   before_action :set_founders, only: [:show, :edit, :update]
  5.   before_action :authenticate_user!
  6.   skip_before_action :verify_authenticity_token
  7.  
  8.   # GET /basic_informations/new
  9.   def new
  10.     @basic_information = BasicInformation.new
  11.   end
  12.  
  13.   # GET /basic_informations/1/edit
  14.   def edit
  15.   end
  16.  
  17.   def destroy
  18.     if params["fl_id"].present?
  19.       Filiation.find(params["fl_id"]).destroy
  20.     end
  21.     if params["fn_id"].present?
  22.       Founder.find(params["fn_id"]).destroy
  23.     end
  24.    
  25.     redirect_back(fallback_location: root_path)
  26.   end
  27.  
  28.   def index
  29.     @basic_informations = BasicInformation.all
  30.   end
  31.  
  32.   # POST /basic_informations
  33.   def create
  34.     if params["create"]=="fil"
  35.       @filiation = Filiation.new(:'owner' => current_user.id)
  36.       @filiation.save
  37.     end
  38.     if params["create"]=="found"
  39.       @founder = Founder.new(:'owner' => current_user.id)
  40.       @founder.save
  41.     end
  42.     redirect_back(fallback_location: root_path)
  43.   end
  44.  
  45.   # PATCH/PUT /basic_informations/1
  46.  
  47.   def update
  48.     founder_params_update
  49.     filiation_params_update
  50.     @basic_information.update(basic_information_params)
  51.    #redirect_to root_path(notice: 'information was successfully created.')
  52.    
  53.  end
  54.  
  55.   private
  56.     # Use callbacks to share common setup or constraints between actions.
  57.     def set_basic_information
  58.       @basic_information = BasicInformation.last
  59.     end
  60.  
  61.     def set_filiations
  62.       @filiations_list = []
  63.       Filiation.all.each do |f|
  64.         @filiations_list << f
  65.       end
  66.     end
  67.  
  68.     def set_founders
  69.       @founders_list = []
  70.       Founder.all.each do |f|
  71.         @founders_list << f
  72.       end
  73.     end
  74.    
  75.     # Never trust parameters from the scary internet, only allow the white list through.
  76.     def basic_information_params
  77.       params.require(:basic_information).permit!
  78.     end
  79.  
  80.     def filiation_params_update
  81.       if params["filiations"].present?
  82.         params["filiations"].each do |filiation|
  83.           if params["filiations"][filiation]["name"] != ""
  84.            @params = params["filiations"][filiation].permit!
  85.            @params["owner"] = current_user.id
  86.            Filiation.find(filiation).update(@params)
  87.           end
  88.         end
  89.       end
  90.     end
  91.  
  92.     def founder_params_update
  93.       if params["founders"].present?
  94.         params["founders"].each do |founder|        
  95.           @params = params["founders"][founder].permit!
  96.           @params["owner"] = current_user.id
  97.            Founder.find(founder).update(@params)
  98.        end
  99.      end
  100.    end  
  101. end
Add Comment
Please, Sign In to add comment