Skip to content

Instantly share code, notes, and snippets.

@fourcolors
Forked from robyurkowski/registration_controller.rb
Created January 8, 2012 03:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fourcolors/1576996 to your computer and use it in GitHub Desktop.
Save fourcolors/1576996 to your computer and use it in GitHub Desktop.
<%= form_tag registration_index_path do %>
<%= fields_for User.new do |u| %>
<%= u.label :phone %>
<%= u.text_field :phone %>
<% end %>
<p>
<%= label_tag :workout %>
<%= check_box_tag :workout %>
</p>
<p>
<%= label_tag :quote %>
<%= check_box_tag :quote %>
</p>
<p>
<%= label_tag :tip %>
<%= check_box_tag :tip %>
</p>
<%= fields_for Reminder.new do |r| %>
<p>
<%= r.label :monday %>
<%= r.check_box :monday %>
</p>
<p>
<%= r.label :tuesday %>
<%= r.check_box :tuesday %>
</p>
<p>
<%= r.label :wednesday %>
<%= r.check_box :wednesday %>
</p>
<p>
<%= r.label :thursday %>
<%= r.check_box :thursday %>
</p>
<p>
<%= r.label :friday %>
<%= r.check_box :friday %>
</p>
<p>
<%= r.label :saturday %>
<%= r.check_box :saturday %>
</p>
<p>
<%= r.label :sunday %>
<%= r.check_box :sunday %>
</p>
<p>
<%= r.label :send_at %>
<%= r.time_select :send_at %>
</p>
<p>
<%= r.label :time_zone %>
<%= r.time_zone_select :time_zone, ActiveSupport::TimeZone.us_zones %>
</p>
<% end %>
<%= submit_tag :submit %>
<% end %>
class RegistrationController < ApplicationController
before_filter :find_user
def create
if @user.save
add_reminders_to @user
else
render :new, :errors => "Something weird"
end
end
private
def find_user
@user = User.find_or_create_by_phone(params[:user][:phone])
@user.save! if @user.new_record?
end
def add_reminders_to( user )
## update user
# Ensure they have selected a notification type
# all is scoped to return workout, quotes and tips in order, first grabs the first.
if params[:workout]
@reminder = user.reminders.build( Workout.all.first.reminders.build( params[:reminder] ).attributes )
if @reminder.save
redirect_to congrats_path
else
render :new, :error => "There was some validation error"
end
end
if params[:quote]
@quote = user.reminders.build( Quote.all.first.reminders.build( params[:reminder] ).attributes )
if @quote.save
redirect_to congrats_path
else
render :new, :error => "There was some validation error"
end
end
if params[:tip]
redirect_to congrats_path
end
if !params[:tip] and !params[:quote] and !params[:workout]
render :new, :errors => "you must add a workout, tip or quote."
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment