LondonGuy
LondonGuy

Reputation: 11098

Ruby on rails redirect_to won't redirect after save, just stays on page (but does save)

Here is my code:

class UsersController < ApplicationController

  def new
    @user = User.new
  end

   def create
      @user = User.new(params[:user])     
      respond_to do |format|
      if @user.save
      format.html { redirect_to :success }  

      else

      format.html { render :new }  
      format.js   { render :form_errors }
      end
    end
  end

end

View:

<div id="joinFormContainer"> 
<%= form_for @user, :remote => true do |f| %>
      <div id="firstNameField">
       <%= f.text_field :first_name, :placeholder => "First Name" %>
      </div>
      <div id="lastNameField">
   <%= f.text_field :last_name, :placeholder => "Last Name" %> 
      </div>
      <div id="emailFieldJoin">
   <%= f.text_field :email, :placeholder => "Email"  %>
      </div>

      <div id="passwordFieldJoin">
   <%= f.password_field :password, :placeholder => "Password"  %>
      </div>
      <div id="usernameField">
   <%= f.text_field :username, :placeholder => "Username"  %>
      </div>

      <div id="joinButton">      <%= f.submit 'Join Us', :id =>"join_submit" %> </div> 
    </div><% end %>
   <div id="error_explanation">     

<%= @user.errors.full_messages.first if @user.errors.any? %>

 </div>
  </div>
  <p>&nbsp;</p>

  </div>
</div>

log:

Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `password_salt`
  SQL (0.9ms)  INSERT INTO "users" ("created_at", "email", "encrypted_password", "first_name", "last_name", "password_salt", "updated_at", "username") VALUES (?, ?, ?, ?, ?, ?, ?, ?)  [["created_at", Wed, 26 Oct 2011 23:40:03 UTC +00:00], ["email", "[email protected]"], ["encrypted_password", "$2a$10$vFOoxHfvc3N2jNPMgx3iN.cNrxENvO5qAaSTaUa5itmzb0uADV9ZS"], ["first_name", "fddffdf"], ["last_name", "dffdffddff"], ["password_salt", "$2a$10$vFOoxHfvc3N2jNPMgx3iN."], ["updated_at", Wed, 26 Oct 2011 23:40:03 UTC +00:00], ["username", "fdgdgddgfdfd"]]
Redirected to http://localhost:3000/success
Completed 302 Found in 87ms


Started GET "/success" for 127.0.0.1 at 2011-10-27 00:40:03 +0100
  Processing by UsersController#success as JS
Rendered users/success.html.erb within layouts/application (0.0ms)
Completed 200 OK in 10ms (Views: 10.1ms | ActiveRecord: 0.0ms)

After save the user isn't redirected to temporary success page. Am I missing something? I also tried redirect_to 'success'

Upvotes: 2

Views: 1778

Answers (2)

LondonGuy
LondonGuy

Reputation: 11098

This simple change to the controller solved my issue:

I replaced:

format.html { redirect_to :success } 

with:

format.js { render :js => "window.location = '#{success_path}'" }

all is working fine now.

Decided to not bother with recaptcha for now

Upvotes: 2

stephenmurdoch
stephenmurdoch

Reputation: 34643

EDIT:

From reading your logs, your form is being processed as JS. If you want the redirect, then remove the remote=>true from your form for now

The rest is my original answer, it may be an idea to implement some of it anyway:


I think you need redirect_to success_path

UPDATE:

ok, this will work, (hopefully):

1- in routes.rb do the following:

resources :users do
  member do
    get 'success'
  end
end

In User controller, create a new action called success

def success
  @user = User.find(params[:id])
end

In Create action

def create
  @user = User.new(params[:user])     
  respond_to do |format|
  if @user.save
    format.html { redirect_to success_user_path(@user) }  
  else
    format.html { render :new }  
    format.js   { render :form_errors }
  end
end

Then create /views/users/success.html.erb (or haml) and put whatever you want in it

Upvotes: 0

Related Questions