master
1class GymsController < ApplicationController
2 before_action { @search_path = gyms_path }
3 before_action only: [:index] { @remote_search = true }
4
5 def index
6 @gyms = paginate(Gym.search_with(params))
7 end
8
9 def show
10 @gym = Gym.find(params[:id])
11 end
12
13 def new
14 @gym = Gym.new
15 @gym.build_location
16 @countries = Carmen::Country.all.sort_by(&:name).map do |x|
17 [x.name, x.code]
18 end
19 end
20
21 def create
22 @gym = build_gym
23
24 if @gym.save
25 respond_to do |format|
26 format.html { redirect_to gyms_path(q: @gym.name) }
27 format.js { render @gym }
28 end
29 else
30 flash[:error] = @gym.errors.full_messages
31 render :new
32 end
33 end
34
35 private
36
37 def secure_params
38 params.require(:gym).permit(
39 :name,
40 :yelp_id,
41 location_attributes: [:address, :city, :region, :country, :postal_code]
42 )
43 end
44
45 def build_gym
46 if params[:yelp_id].present?
47 Gym.create_from_yelp!(params[:yelp_id])
48 else
49 Gym.new(secure_params)
50 end
51 end
52end