In my Rails 3 app, for some reason I’m getting a 302 in my registration process. Specifically, when I redirect from creating a new User to editing their Profile I get redirected out of the app to /login. This only happens on Heroku. If I run the app locally, it works as it should.
Here’s my heroku logs:
2012-02-10T02:22:05+00:00 app[web.1]: Started POST "/users" for 98.218.231.113 at 2012-02-10 02:22:05 +0000
2012-02-10T02:22:05+00:00 app[web.1]:
2012-02-10T02:22:05+00:00 app[web.1]:
2012-02-10T02:22:06+00:00 app[web.1]: Processing by UsersController#create as HTML
2012-02-10T02:22:06+00:00 app[web.1]: [paperclip] Saving attachments.
2012-02-10T02:22:06+00:00 app[web.1]: Rendered user_mailer/registration_confirmation.text.erb (0.4ms)
2012-02-10T02:22:06+00:00 app[web.1]: Completed 302 Found in 964ms
2012-02-10T02:22:06+00:00 app[web.1]: Sent mail to andy@dundermifflin.com (694ms)
2012-02-10T02:22:06+00:00 heroku[router]: POST myapp.org/users dyno=web.1 queue=0 wait=0ms service=984ms status=302 bytes=97
2012-02-10T02:22:06+00:00 app[web.1]: Redirected to http://myapp.org/signup/join
2012-02-10T02:22:06+00:00 app[web.1]: Parameters: {"utf8"=>"✓", "authenticity_token"=>"4PjdAx5aaSph3KBDQfiPlJTlvTsh+DDTF1x+S7Ol2jc=", "user"=>{"profile_attributes"=>{"first_name"=>"Andrew", "last_name"=>"Bernard", "bio"=>""}, "email"=>"andy@dundermifflin.com", "password"=>"[FILTERED]"}, "commit"=>"Sign Up For myapp"}
2012-02-10T02:22:06+00:00 app[web.1]: Rendered user_mailer/registration_confirmation.html.erb (0.6ms)
2012-02-10T02:22:06+00:00 app[web.1]:
2012-02-10T02:22:06+00:00 heroku[nginx]: 98.218.231.113 - - [10/Feb/ 2012:02:22:06 +0000] "POST /users HTTP/1.1" 302 97 "http://myapp.org/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/534.53.11 (KHTML, like Gecko) Version/5.1.3 Safari/534.53.10" myapp.org
2012-02-10T02:22:06+00:00 app[web.1]: Started GET "/signup/join" for 98.218.231.113 at 2012-02-10 02:22:06 +0000
2012-02-10T02:22:06+00:00 app[web.1]:
2012-02-10T02:22:06+00:00 app[web.1]:
2012-02-10T02:22:06+00:00 heroku[router]: GET myapp.org/signup/join dyno=web.1 queue=0 wait=0ms service=29ms status=302 bytes=91
2012-02-10T02:22:06+00:00 app[web.1]: Processing by ProfilesController#edit as HTML
2012-02-10T02:22:06+00:00 app[web.1]: Redirected to http://myapp.org/login
2012-02-10T02:22:06+00:00 app[web.1]: Completed 302 Found in 12ms
2012-02-10T02:22:06+00:00 heroku[nginx]: 98.218.231.113 - - [10/Feb/ 2012:02:22:06 +0000] "GET /signup/join HTTP/1.1" 302 91 "http://myapp.org/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/534.53.11 (KHTML, like Gecko) Version/5.1.3 Safari/534.53.10" myapp.org
2012-02-10T02:22:06+00:00 app[web.1]:
2012-02-10T02:22:06+00:00 app[web.1]:
2012-02-10T02:22:06+00:00 app[web.1]: Started GET "/login" for 98.218.231.113 at 2012-02-10 02:22:06 +0000
2012-02-10T02:22:06+00:00 app[web.1]: Processing by SessionsController#new as HTML
2012-02-10T02:22:06+00:00 app[web.1]: Rendered layouts/_footer.html.erb (0.6ms)
2012-02-10T02:22:06+00:00 heroku[router]: GET myapp.org/login dyno=web.1 queue=0 wait=0ms service=27ms status=200 bytes=3062
In my users_controller.rb:
def create
@user = User.new(params[:user])
if @user.save
session[:user_id] = @user.id
redirect_to join_path, :notice => 'User successfully added.'
UserMailer.registration_confirmation(@user).deliver
else
render :action => 'new'
end
end
In my routes.rb:
match "/signup/join" => "profiles#edit", :as => 'join'
So if I look at my logs, I’m submitting a POST to /users, sending an email, and redirecting to the edit action in profiles_controller.rb. So it seems like everything should be correct but I’m getting redirected. Has anyone had this happen to them too?
UPDATE: In my profiles_controller.rb:
class ProfilesController < ApplicationController
before_filter :authenticate, :only => [:edit, :update]
layout "application", :except => [:edit, :show]
def user
@user = current_user
end
def edit
@profile = user.profile
render :layout => "join_form"
end
In my sessions_controller.rb:
class SessionsController < ApplicationController
def new
end
def create
if user = User.authenticate(params[:email].downcase, params[:password])
session[:user_id] = user.id
cookies.permanent[:auth_token] = user.auth_token
if user.profile.higher_ed?
redirect_to user.profile, :notice => "Logged in successfully"
else
redirect_to join_path, :notice => "Logged in successfully"
end
else
flash.now[:alert] = "Invalid login/password. Try again!"
render :action => 'new'
end
end
My gemfile:
source "http://rubygems.org"
gem "aws-s3", :require => "aws/s3"
gem "aws-sdk"
gem "cancan"
gem "cocaine"
gem "fastercsv"
gem "nifty-generators"
gem "jquery-rails", ">= 1.0.12"
gem "paperclip"
gem "rails", "3.0.5"
gem "rake", "0.9.2"
gem "ransack"
gem "twitter"
gem "mocha", :group => :test
group :production, :development, :test do
gem "pg"
end
gem "pg"
As @phoet suggested above, I started removing code that was potentially causing issues and I got a no method error, which led me to my solution. I had to rework the logic in my
current_usermethod. I changed it from:To: