From 7c753fd5c9249f562b3b56c6519daf9bbfc3acef Mon Sep 17 00:00:00 2001 From: mehulkar Date: Sat, 6 Jun 2015 00:05:17 -0500 Subject: [PATCH 1/2] Move City suggestion code to a separate controller --- app/controllers/cities_controller.rb | 29 ++----------------- .../city_suggestions_controller.rb | 26 +++++++++++++++++ .../_user_form.html.haml | 2 +- .../new.html.erb} | 2 +- config/routes.rb | 4 +-- 5 files changed, 32 insertions(+), 31 deletions(-) create mode 100644 app/controllers/city_suggestions_controller.rb rename app/views/{cities => city_suggestions}/_user_form.html.haml (97%) rename app/views/{cities/forbes_new.html.erb => city_suggestions/new.html.erb} (99%) diff --git a/app/controllers/cities_controller.rb b/app/controllers/cities_controller.rb index 4799f8f0..de1ab180 100644 --- a/app/controllers/cities_controller.rb +++ b/app/controllers/cities_controller.rb @@ -1,38 +1,13 @@ class CitiesController < ApplicationController - before_action :set_city, except: [:index, :new, :index, :forbes_new, :forbes_create] + before_action :set_city, except: [:index, :new, :index] before_action :authenticate_user!, :authorized?, only: [:new, :create, :edit, :update, :destroy] - before_action :away_ye_waitlisted, except: [:index, :forbes_show, :forbes_new, :forbes_suggest, :forbes_create, :forbes_set_city] + before_action :away_ye_waitlisted, except: [:index, :forbes_show, :forbes_set_city] # GET /cities def index use_new_styles end - def forbes_new - use_new_styles - if current_user - @city = City.new - else - flash[:notice] = 'Sign up first so we can let you know when your city is approved!' - redirect_to sign_up_path - end - end - - def forbes_create - #NOTE: Seems unnecessary but required for the error case - use_new_styles - - @city = NewSuggestedCity.call(city_params, current_user) - - respond_to do |format| - if @city.persisted? - format.html { redirect_to forbes_city_path(@city), notice: 'We\'ve gotten your submission and will get to it ASAP.' } - else - format.html { render :forbes_new, alert: "Something went wrong! Our fault. Could you try again?" } - end - end - end - def forbes_set_city current_user.update(home_city: @city) redirect_to forbes_city_path(@city) diff --git a/app/controllers/city_suggestions_controller.rb b/app/controllers/city_suggestions_controller.rb new file mode 100644 index 00000000..282c8cc3 --- /dev/null +++ b/app/controllers/city_suggestions_controller.rb @@ -0,0 +1,26 @@ +class CitySuggestionsController < ApplicationController + def new + use_new_styles + if current_user + @city = City.new + else + flash[:notice] = 'Sign up first so we can let you know when your city is approved!' + redirect_to sign_up_path + end + end + + def create + use_new_styles + @city = NewSuggestedCity.call(city_params, current_user) + if @city.persisted? + redirect_to forbes_city_path(@city), notice: 'We\'ve gotten your submission and will get to it ASAP.' + else + render :new, alert: "Something went wrong! Our fault. Could you try again?" + end + end + +private + def city_params + params.require(:city).permit! + end +end \ No newline at end of file diff --git a/app/views/cities/_user_form.html.haml b/app/views/city_suggestions/_user_form.html.haml similarity index 97% rename from app/views/cities/_user_form.html.haml rename to app/views/city_suggestions/_user_form.html.haml index b6cccb9e..d54cbf36 100644 --- a/app/views/cities/_user_form.html.haml +++ b/app/views/city_suggestions/_user_form.html.haml @@ -14,4 +14,4 @@ %h2= "#{pluralize(@city.errors.count, "error")} prohibited this city from being saved:" %ul - @city.errors.full_messages.each do |msg| - %li= msg + %li= msg \ No newline at end of file diff --git a/app/views/cities/forbes_new.html.erb b/app/views/city_suggestions/new.html.erb similarity index 99% rename from app/views/cities/forbes_new.html.erb rename to app/views/city_suggestions/new.html.erb index 4eed30f3..8c4f0ae0 100644 --- a/app/views/cities/forbes_new.html.erb +++ b/app/views/city_suggestions/new.html.erb @@ -21,4 +21,4 @@ <%= render partial: 'shared/new_footer' %> - + \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index a05d9268..66f17c2d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -40,8 +40,8 @@ resources :cities do collection do - get '/suggest' => 'cities#forbes_new', as: :suggest - post '/suggest' => 'cities#forbes_create', as: :suggest_create + get '/suggest' => 'city_suggestions#new', as: :suggest + post '/suggest' => 'city_suggestions#create', as: :suggest_create end member do get '/host/:host_id' => 'hosts#show', as: :host From 38688d8d3c78a09b6032c35a1fe185286055304c Mon Sep 17 00:00:00 2001 From: mehulkar Date: Sat, 6 Jun 2015 00:26:08 -0500 Subject: [PATCH 2/2] Combine city suggestion templates. No need to put the form in a partial --- .../city_suggestions/_user_form.html.haml | 17 ------------ app/views/city_suggestions/new.html.erb | 26 ++++++++++++++++++- 2 files changed, 25 insertions(+), 18 deletions(-) delete mode 100644 app/views/city_suggestions/_user_form.html.haml diff --git a/app/views/city_suggestions/_user_form.html.haml b/app/views/city_suggestions/_user_form.html.haml deleted file mode 100644 index d54cbf36..00000000 --- a/app/views/city_suggestions/_user_form.html.haml +++ /dev/null @@ -1,17 +0,0 @@ -.submit-city-container - = form_for @city, html: {class: 'dummy new_city'}, url: suggest_create_cities_path do |f| - = f.text_field :name, placeholder: 'Your city or college name' - = f.submit 'Submit', data: { confirmation: false }, class: 'save-city' - .city-confirmation-text - %h2.confirm-message - Double check your spelling! And make sure it’s just the city or school name. - %p - After you confirm, we’ll make sure everything checks out and email you - when your friends and neighbors can join you for Tea With Strangers. - - if @city.errors.any? - - #error_explanation - %h2= "#{pluralize(@city.errors.count, "error")} prohibited this city from being saved:" - %ul - - @city.errors.full_messages.each do |msg| - %li= msg \ No newline at end of file diff --git a/app/views/city_suggestions/new.html.erb b/app/views/city_suggestions/new.html.erb index 8c4f0ae0..76e2bd13 100644 --- a/app/views/city_suggestions/new.html.erb +++ b/app/views/city_suggestions/new.html.erb @@ -7,7 +7,31 @@

Let's bring TWS to new lands!

If you didn’t find your city in the list before, add it here!

- <%= render 'user_form' %> +
+ <%= form_for @city, html: {class: 'dummy new_city'}, url: suggest_create_cities_path do |f| %> + <%= f.text_field :name, placeholder: 'Your city or college name' %> + <%= f.submit 'Submit', data: { confirmation: false }, class: 'save-city' %> + <% end %> +
+

+ Double check your spelling! And make sure it’s just the city or school name. +

+

+ After you confirm, we’ll make sure everything checks out and email you + when your friends and neighbors can join you for Tea With Strangers. +

+
+ <% if @city.errors.any? %> +
+

<%="#{pluralize(@city.errors.count, "error")} prohibited this city from being saved:"%>

+
    + <%= @city.errors.full_messages.each do |msg| %> +
  • <%= msg %>
  • + <% end %> +
+
+ <% end %> +

Or click your city below if we're already there