Last active
August 29, 2015 14:01
-
-
Save mikeadeleke/6ae16e67216c37f17737 to your computer and use it in GitHub Desktop.
Accept Invite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
invite_mailer.rb | |
class InviteMailer < ActionMailer::Base | |
default from: "[email protected]" | |
def invite_notification(invite, email) | |
@invite = invite | |
@property = @invite.property | |
@url = invite_url(@invite) | |
mail(to: @invite.email, subject: "You've been added as a roommate!") | |
end | |
private | |
def invite_url(invite) | |
"localhost:3000/invites/accept_invitation?id=#{@invite.id}" | |
end | |
end | |
application_controller.rb | |
class ApplicationController < ActionController::Base | |
# Prevent CSRF attacks by raising an exception. | |
# For APIs, you may want to use :null_session instead. | |
protect_from_forgery with: :exception | |
before_filter :make_profile | |
before_filter :configure_permitted_parameters, if: :devise_controller? | |
layout 'cerulean' | |
def after_sign_up_path_for(resource) | |
print params | |
"localhost:3000/invites/accept_invitation?id=#{@invite.id}" | |
end | |
private | |
def make_profile | |
if user_signed_in? and current_user.profile.year.nil? | |
redirect_to new_profile_path | |
end | |
end | |
#-> Prelang (user_login:devise) | |
def require_user_signed_in | |
unless user_signed_in? | |
# If the user came from a page, we can send them back. Otherwise, send | |
# them to the root path. | |
if request.env['HTTP_REFERER'] | |
fallback_redirect = :back | |
elsif defined?(root_path) | |
fallback_redirect = root_path | |
else | |
fallback_redirect = "/" | |
end | |
redirect_to fallback_redirect, flash: {error: "You must be signed in to view this page."} | |
end | |
end | |
def configure_permitted_parameters | |
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation, :image, :name, :phone_number, :property_id) } | |
end | |
def mobile_device? | |
request.user_agent =~ /Mobile|webOS/ | |
end | |
helper_method :mobile_device? | |
end | |
registrations_controller.rb | |
class Users::RegistrationsController < Devise::RegistrationsController | |
include ApplicationHelper | |
def create | |
super | |
set_property | |
end | |
def new | |
super | |
end | |
def edit | |
super | |
end | |
def after_user_signed_in | |
end | |
def set_property | |
@property = Property.find(params[:property_id]) | |
current_user.properties << @property | |
puts @property | |
puts current_user.properties | |
end | |
end | |
new.html.erb (registrations) | |
<div class="row"> | |
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> | |
<%= devise_error_messages! %> | |
<form class="form-horizontal"> | |
<fieldset> | |
<legend>Sign up</legend> | |
<br>or <%= link_to "Sign in", new_session_path(resource_name) %><br /> | |
<%= f.hidden_field :name, value: "name" %> | |
<%= hidden_field_tag :property_id, params[:property_id] %> | |
<div class="form-group"> | |
<label for="inputEmail" class="col-lg-2 control-label">Avatar</label> | |
<div class="col-lg-10"> | |
<%= f.file_field :avatar, class: "form-control" %> | |
</div> | |
</div> | |
<div class="form-group"> | |
<label for="inputEmail" class="col-lg-2 control-label">Email</label> | |
<div class="col-lg-10"> | |
<%= f.email_field :email, autofocus: true, placeholder: "Enter your Drexel email. Ex: [email protected]", class: "form-control", id: "inputEmail" %> | |
</div> | |
</div> | |
<div class="form-group"> | |
<label for="inputPassword" class="col-lg-2 control-label">Password</label> | |
<div class="col-lg-10"> | |
<%= f.password_field :password, autocomplete: "off", placeholder: "please don't say password", class: "form-control" %> | |
</div> | |
</div> | |
<div class="form-group"> | |
<label for="inputPassword" class="col-lg-2 control-label">Password Confirmation</label> | |
<div class="col-lg-10"> | |
<%= f.password_field :password_confirmation, autocomplete: "off", placeholder: "we're warning you", class: "form-control" %> | |
</div> | |
</div> | |
<div class="form-group"> | |
<label for="inputPassword" class="col-lg-2 control-label">Phone number</label> | |
<div class="col-lg-10"> | |
<%= f.text_field :phone_number, placeholder: "Your number will always remain anonymous", class: "form-control", id: "inputEmail" %> | |
</div> | |
</div> | |
<div class="form-group"> | |
<div class="col-lg-10 col-lg-offset-2"> | |
<%= f.submit "Sign up", class: "btn btn-primary" %> | |
</div> | |
</div> | |
</fieldset> | |
</form> | |
<% end %> | |
<%= render "devise/shared/links" %><br><br> | |
<a href="http://eepurl.com/RClAb">Wait, I'm a landlord!</a> / <a href="http://eepurl.com/RCkWr">Wait, I'm not a Drexel student!</a><br><br><br><br><br><br><br> | |
</div> | |
</div> | |
invites_controller.rb | |
class InvitesController < ApplicationController | |
def new | |
load_property | |
@invite = @property.invites.build | |
end | |
def create | |
load_property | |
@invite = @property.invites.new(invite_params) | |
if @invite.save | |
invite_roommates | |
redirect_to @property.listing | |
else | |
render :new | |
end | |
end | |
def accept_invitation | |
@invite = Invite.find(params[:id]) | |
@property = @invite.property | |
redirect_to new_user_registration_path(property_id: @property.id) | |
end | |
private | |
def load_property | |
@property = Property.find(params[:property_id]) | |
end | |
def invite_roommates | |
emails = params[:invite][:email].split(',').map(&:strip).select(&:present?) | |
emails.each do |email| | |
InviteMailer.invite_notification(@invite, @invite.email).deliver | |
end | |
end | |
def invite_params | |
params.require(:invite).permit(:token, :email) | |
end | |
end | |
Advice: | |
Here's what you should try to tease out: | |
1) Is your set method not working | |
As in, is there a better way to assign the variable. Even if you use forceful methods like the one's I'm proposing above, you simply want to figure out if the object you're assigning it to is protected or immutable | |
2) Are you saving the property_id to the database properly. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment