Guest User

Untitled

a guest
Oct 17th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. 1 error prohibited this search from being saved:
  2. - Areas is invalid
  3.  
  4. class SearchesController < ApplicationController
  5.  
  6. before_action :set_search, only: [:show, :edit, :update, :destroy]
  7. before_action :require_admin, only: [:index]
  8.  
  9. def create
  10. @search = Search.new(search_params)
  11. @search.max_price = search_params["max_price"].gsub('$', '').gsub(',', '')
  12. @contact = Contact.new #in case it gets rerouted back to search#new
  13.  
  14. respond_to do |format|
  15. if @search.save
  16. if @search.school_slug.present?
  17. # # format.html { redirect_to @school(:search_id => @search.id), notice: "#{@school.name} search updated"}
  18. @school = School.find_by(slug: @search.school_slug)
  19. p @school.name
  20. p @school.id
  21. # format.html { redirect_to @school(:search_id => @search.id), notice: "#{@school.name} search updated"}
  22. format.html { redirect_to school_path(slug: @school.slug, city: @school.city.parameterize.truncate(80, omission: ''), :search_id => @search.id), notice: "#{@school.name} search updated"}
  23. else
  24. format.html { redirect_to @search, notice: 'Search was successfully created.' }
  25. format.json { render :show, status: :created, location: @search }
  26. end
  27. else
  28. format.html { render :new }
  29. format.json { render json: @search.errors.full_messages, status: :unprocessable_entity }
  30. end
  31. end
  32. end
  33.  
  34. private
  35. # Use callbacks to share common setup or constraints between actions.
  36. def set_search
  37. @search = Search.find(params[:id])
  38. end
  39.  
  40. # Never trust parameters from the scary internet, only allow the white list through.
  41. def search_params
  42. params.require(:search).permit(:school_id, :beds, :max_price, :user_id, :name, :district_id, :city_id, :email_me, :rental, :grade, :school_slug, area_ids: [], :prop_type => [])
  43. end
  44. end
  45.  
  46. irb(main):004:0> a = Search.new(beds: "3", max_price: "450000", area_ids:["1","2"])
  47. Area Load (0.3ms) SELECT "areas".* FROM "areas" WHERE "areas"."id" IN (?, ?) [["id", 1], ["id", 2]]
  48. => #<Search id: nil, beds: 3, max_price: 450000, prop_type: nil, user_id: nil, name: nil, area_id: nil, school_id: nil, district_id: nil, city_id: nil, email_me: nil, rental: nil, created_at: nil, updated_at: nil, grade: nil, school_slug: nil>
  49. irb(main):005:0> a.areas
  50. => #<ActiveRecord::Associations::CollectionProxy [#<Area id: 1, name: "N", created_at: "2018-05-24 12:24:16", updated_at: "2018-10-10 16:22:16", msa_id: 1, state_id: nil>, #<Area id: 2, name: "NW", created_at: "2018-05-24 12:24:16", updated_at: "2018-10-10 16:22:16", msa_id: 1, state_id: nil>]>
  51. irb(main):006:0> a.save
  52. (0.2ms) begin transaction
  53. Msa Load (0.5ms) SELECT "msas".* FROM "msas" WHERE "msas"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
  54. Msa Load (0.1ms) SELECT "msas".* FROM "msas" WHERE "msas"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
  55. (0.2ms) rollback transaction
  56. => false
  57. irb(main):007:0> a.errors
  58. => #<ActiveModel::Errors:0x00007fb245504958 @base=#<Search id: nil, beds: 3, max_price: 450000, prop_type: nil, user_id: nil, name: nil, area_id: nil, school_id: nil, district_id: nil, city_id: nil, email_me: nil, rental: nil, created_at: nil, updated_at: nil, grade: nil, school_slug: nil>, @messages={:areas=>["is invalid"]}, @details={:areas=>[{:error=>:invalid}, {:error=>:invalid}]}>
  59.  
  60. class AreasController < ApplicationController
  61.  
  62. def index(:msa_id)
  63. @areas = Area.where(msa_id: params[:msa_id])
  64. end
  65.  
  66. def show
  67. end
  68.  
  69. private
  70. # Use callbacks to share common setup or constraints between actions.
  71. def set_area
  72. @area = Area.find(params[:id])
  73. end
  74.  
  75. # Never trust parameters from the scary internet, only allow the white list through.
  76. def area_params
  77. params.require(:area).permit(:name, :msa_id, :state_id, district_ids:[], search_ids:[], county_ids:[])
  78. end
  79. end
  80.  
  81. class Area < ApplicationRecord
  82.  
  83. has_many :districts
  84. has_many :counties
  85. has_and_belongs_to_many :searches
  86. belongs_to :msa
  87. belongs_to :state
  88.  
  89. end
  90.  
  91. create_table "searches", force: :cascade do |t|
  92. t.integer "beds"
  93. t.integer "max_price"
  94. t.string "prop_type"
  95. t.integer "user_id"
  96. t.string "name"
  97. t.integer "area_id"
  98. t.integer "school_id"
  99. t.integer "district_id"
  100. t.integer "city_id"
  101. t.boolean "email_me"
  102. t.boolean "rental"
  103. t.datetime "created_at", null: false
  104. t.datetime "updated_at", null: false
  105. t.string "grade"
  106. t.string "school_slug"
  107. end
Add Comment
Please, Sign In to add comment