Advertisement
Guest User

Untitled

a guest
Oct 5th, 2015
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. class PriestProfilesController < ApplicationController
  2. before_action :set_priest_profile, only: [:show, :edit, :update, :destroy]
  3. before_action :set_areaids, only: [:create]
  4. def update_cities
  5. @areas = Area.where("city_id = ?", params[:city_id])
  6.  
  7. respond_to do |format|
  8. format.js
  9. end
  10.  
  11. end
  12. # GET /priest_profiles
  13. # GET /priest_profiles.json
  14. def index
  15. @priest_profiles = PriestProfile.all
  16. end
  17.  
  18. # GET /priest_profiles/1
  19. # GET /priest_profiles/1.json
  20. def show
  21. @listOfCities= City.all
  22. end
  23.  
  24. # GET /priest_profiles/new
  25. def new
  26. @priest_profile = PriestProfile.new
  27. @cities = City.all.order(:name)
  28. @areas = Area.where("city_id = ?", City.first.id).order(:name)
  29.  
  30. end
  31.  
  32. # GET /priest_profiles/1/edit
  33. def edit
  34. end
  35.  
  36. # POST /priest_profiles
  37. # POST /priest_profiles.json
  38.  
  39. def create
  40. @priest_profile = PriestProfile.new(priest_profile_params)
  41.  
  42. respond_to do |format|
  43. if @priest_profile.save
  44. @priest_profile.areas << @areaids
  45.  
  46. format.html { redirect_to @priest_profile, notice: 'Priest profile was successfully created.' }
  47. format.json { render :show, status: :created, location: @priest_profile }
  48. else
  49. format.html { render :new }
  50. format.json { render json: @priest_profile.errors, status: :unprocessable_entity }
  51. end
  52. end
  53. end
  54.  
  55. # PATCH/PUT /priest_profiles/1
  56. # PATCH/PUT /priest_profiles/1.json
  57. def update
  58. respond_to do |format|
  59. if @priest_profile.update(priest_profile_params)
  60. format.html { redirect_to @priest_profile, notice: 'Priest profile was successfully updated.' }
  61. format.json { render :show, status: :ok, location: @priest_profile }
  62. else
  63. format.html { render :edit }
  64. format.json { render json: @priest_profile.errors, status: :unprocessable_entity }
  65. end
  66. end
  67. end
  68.  
  69. # DELETE /priest_profiles/1
  70. # DELETE /priest_profiles/1.json
  71. def destroy
  72. @priest_profile.destroy
  73. respond_to do |format|
  74. format.html { redirect_to priest_profiles_url, notice: 'Priest profile was successfully destroyed.' }
  75. format.json { head :no_content }
  76. end
  77. end
  78.  
  79. private
  80. # Use callbacks to share common setup or constraints between actions.
  81. def set_priest_profile
  82. @priest_profile = PriestProfile.find(params[:id])
  83. end
  84.  
  85. def set_areaids
  86. x = params[:priest_profile][:area_id]
  87. x.shift(0) #due to hidden field one empty initial element
  88. @areaids = Area.find(x)
  89. end
  90. # Never trust parameters from the scary internet, only allow the white list through.
  91. def priest_profile_params
  92. params.require(:priest_profile).permit(:name, :phone_wrk, :phone_pr, :religion, :icon, :brief, :description, :area_ids, :area_id, :city_id)
  93. end
  94. end
  95.  
  96. <%= form_for(@priest_profile) do |f| %>
  97. <div class="field">
  98. <%= f.label :name %><br>
  99. <%= f.text_field :name %>
  100. </div>
  101.  
  102. <div class="field">
  103. <%= f.label :phone_wrk %><br>
  104. <%= f.text_field :phone_wrk %>
  105. </div>
  106. <!--=================================================-->
  107. <h3> <%= f.select :city_id, options_for_select(@cities.collect { |city|
  108. [city.name.titleize, city.id] }), {include_blank: "(Select City)"}, { id: 'cities_select'} %>
  109.  
  110.  
  111. <%= f.select :area_id, options_for_select(@areas.collect { |area|
  112. [area.name.titleize, area.id] }, 0), {},
  113. {id: 'areas_select',multiple: true}%></h3>
  114. <!--=================================================-->
  115. <br />
  116. <div class="actions">
  117. <%= f.submit %>
  118. </div>
  119. <% end %>
  120.  
  121. class PriestProfile < ActiveRecord::Base
  122. validates :phone_wrk,:name, presence: true
  123. has_many :priest_areaships
  124. has_many :areas, through: :priest_areaships
  125. attr_accessor :city_id, :area_id, :area_ids
  126. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement