mardi 27 septembre 2016

Rails, redirect work in development but not in production

I have a simple form for login, and I want to redirect to admin/index if the login is successful. This works in development but not in production. My production.log says nothing about this, just:

I, [2016-09-27T12:41:53.504340 #7503] INFO -- : Started POST "/login" for 127.0.0.1 at 2016-09-27 12:41:53 +0300 I, [2016-09-27T12:41:53.506417 #7503] INFO -- : Processing by AdminController#login as JS I, [2016-09-27T12:41:53.506698 #7503] INFO -- : Parameters: {"utf8"=>"✓", "admin"=>{"username"=>"testusername", "password"=>"[FILTERED]"}} I, [2016-09-27T12:41:53.618386 #7503] INFO -- : Redirected to http://localhost:3000/admin/index I, [2016-09-27T12:41:53.618978 #7503] INFO -- : Completed 200 OK in 112ms (ActiveRecord: 9.2ms)

I although get a wierd reponse to my login post on firebug:

Turbolinks.clearCache()

Turbolinks.visit("http://localhost:3000/admin/index", {"action":"replace"})

I dont know what that means but could it be that I got a problem with my turbolinks??

Here is my form:

<%= form_for(:admin,:remote => true, :html => {id: 'form-signin', class: 'form-signin'}, :url => 'login') do |f| %>

        <div class="popup-header">

            <span class="text-semibold">Administrator Login</span>

        </div>
        <div class="well">
            <div class="form-group has-feedback">
                <label>Username</label>
                <%= f.text_field :username, class: 'form-control',:html => { type:'text',  name: 'username', required: "", autofocus: ""}, :placeholder => "Username"%>
                <i class="icon-users form-control-feedback"></i>
            </div>

            <div class="form-group has-feedback">
                <label>Password</label>
                <%= f.password_field :password, class: 'form-control', :html => {type:'password', name: 'password', required: ""}, :placeholder => "Password" %>
                <i class="icon-lock form-control-feedback"></i>
            </div>

            <div class="row form-actions">

                <div class="col-xs-6">
                    <button type="submit" class="btn btn-warning pull-right"><i class="icon-menu2"></i> Sign in</button>
                </div>
            </div>
        </div>
        <% end %>
</div>  

And the my Admin_controller:

class AdminController < ApplicationController

def index
    respond_to do |format|
        format.js
    end
end

def login
  if params[:admin][:username].present? && params[:admin][:password].present?
    found_user = Admin.where(:username => params[:admin][:username]).first
      if found_user

        authorized_user = found_user.authenticate(params[:admin][:password])
        session[:admin]=params[:admin][:username]
      end
  end
  if authorized_user
    redirect_to admin_index_path     
  else      
    respond_to do |format|
            format.js {render :action => 'wrong_credentials'}
    end
  end
end

def destroy #LOGOUT FUNCTION
  session[:admin] = nil
  redirect_to :controller => 'admin', :action => 'login_page'
end
end

And my routes

 Rails.application.routes.draw do
  root 'admin#login_page'

  get '/init' => 'api#api_init'

  get '/admin/index', to: 'admin#index', :as => 'admin_index'
  post '/login' => 'admin#login', :as => 'admin_login'
  controller :admin do
  get 'index' => :index
  get  'login' => :new
  post 'login' => :create
  delete 'logout' => :destroy
  end

end

Any help appreciated. Thanks for reading.

Aucun commentaire:

Enregistrer un commentaire