在webrick下工作但不在乘客下工作 [英] working under webrick but not under passenger

查看:55
本文介绍了在webrick下工作但不在乘客下工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


我刚刚通过使用Rails进行Agile Web开发的

指令向我的rails应用程序添加了登录功能。但现在我在

乘客/ apache下得到了

奇怪的错误(看起来像某种路由问题)。它在webrick下工作正常。


错误是:

500内部服务器错误

服务器遇到内部错误或配置错误,并且

无法完成您的请求。


来自日志:


?[4; 36 ; 1mSQL(0.3ms)?[0m?[0; 1mSET SQL_AUTO_IS_NULL = 0?[0m


处理****控制器#index(2010-04-为192.168.1.101) 26

18:56:24)[获取]

?[4; 35; 1mUser列(1.7ms)?[0m?[0mSHOW来自`users`? [0m

?[4; 36; 1mUser负载(0.7ms)?[0m?[0; 1mSELECT *来自`users` WHERE

(`users`` id` IS NULL)LIMIT 1?[0m

重定向到https:// ****** / login / login_page

过滤链停止为[:au​​thorize] rendered_or_redirected。

在31ms完成(DB:3)| 302找到[https:// ****** /]


有没有人有任何建议我可以做些什么来解决这个问题?


--------------------------------------------

类LoginController< ApplicationController


def add_user

@user = User.new(params [:user])

if request.post?和@ user.save

flash.now [:notice] =" User #{@user.name} created"

@user = User.new

结束

结束


def login_page

session [:user_id] = nil

if request.post?

user = User.authenticate(params [:name],params [:password])

if user

session [:user_id] = user.id

session [:user_name] = user.name

uri = session [:original_uri]

session [:original_uri] = nil

redirect_to(uri || {:action =>" index"})

else

flash [:notice] ="无效的用户/密码组合"

结束

结束

结束


def logout

session [:user_id] = nil

flash [:notice] ="退出"

redirect_to(:action =>" login_page")

结束


def index

结束


def delete_user

if request.post ?

user = User.find(params [:id])

如果User.count == 1

flash [:notice] = 你不能删除最后剩下的用户!

其他

user.destroy

结束

结束

redirect_to(:action => :list_users)

结束


def list_users

@all_users = User.find(:all)

结束

结束

------------------------------ ------------


类ApplicationController< ActionController :: Base

before_filter:authorize,:except => :login_page

helper:所有#include所有助手,所有时间

protect_from_forgery#请参阅ActionController :: RequestForgeryProtection

了解详情

def create_default_variables(ctrl_name)

session [:ctrl_name] = ctrl_name

end

#从日志中清除敏感参数

#filter_parameter_logging:密码

private

def authorize

除非User.find_by_id(session [:user_id])

session [:original_uri] = request.request_uri

flash [:notice] ="请登录"

redirect_to(:controller => " login",:action =>" login_page")

end

end

end

------------------------------------------------ <无线电通信/>
需要''digest / sha1''

class用户< ActiveRecord :: Base

validates_presence_of:name

validates_uniqueness_of:name

attr_accessor:password_confirmation

validates_confirmation_of:password


def验证

errors.add_to_base(缺少密码)ifhed_pa​​ssword.blank?

end


def self.authenticate(名称,密码)

user = self.find_by_name(name)

if user

expected_pa​​ssword = encrypted_pa​​ssword(密码,user.salt)

如果user.hashed_pa​​ssword!= expected_pa​​ssword

user = nil

end

结束

用户

结束


#''密码''是虚拟属性

def密码

@password

结束

def密码=(pwd)

@password = pwd

create_new_salt

self.hashed_pa​​ssword = User.encrypted_pa​​ss单词(self.password,

self.salt)

结束


def after_destroy

if User.count.zero?

加注无法删除最后一位用户

结束

结束

private

def self.encrypted_pa​​ssword(密码,盐)

string_to_hash =密码+" wibble" +盐#''wibble''使它成为

难以猜测

摘要:: SHA1.hexdigest(string_to_hash)

end


def create_new_salt

self.salt = self.object_id.to_s + rand.to_s

end

end

-----------------------------------------

login_page.html.erb

< div class =" user-form">

< fieldset>

< legend>请登录< / legend>

<%form_tag do%>

< p>

< label for =" name">名称:< / label>

<%= text_field_tag:name,params [:name]%>

< / p>

< p>

< label for =" password">密码:< / label>

<%= password_field_tag:password,params [:password]%>

< / p>

< p><%= submit_tag"日志在" %>< / p>

<%end%>

< / fieldset>

< / div>

-------------------------------------------

routes.rb

ActionController :: Routing :: Routes.draw do | map |


map。资源:controller1

map.resources:controller2

...


map.root:controller => controller1


map.connect'':controller /:action /:id''

map.connect'':controller /:action / :id。:格式''

结束


---------------------- ---------------------

和apache日志:


[错误] [客户端192.168.1.101]由于可能的配置错误,请求超出了10个内部
重定向的限制。如果需要,使用

''LimitInternalRecursion''来增加限制。使用

''LogLevel debug''来获得回溯。


" LogLevel debug"没有提供任何更多信息。


更新:



肯定是路由问题。

我可以通过添加以下内容来部分解决问题:


map.connect''login'',: controller => ''login'',:action => login_page

map.connect''login / list_users'',: controller => " login",:action => " list_users"

map.connect''login / add_user'',: controller => " login",:action => add_user

map.connect''login / logout'',: controller => " login",:action => logout


到routes.rb并且第一行正常工作。

因此重定向到< app> / login或直接输入从登录控制器给出login_page动作。

剩下的问题是我仍然不能直接使用任何动作,如/ login / list_users甚至登录/ login_page。

Hi!

I have just added login feature to my rails application by following the
instructions from "Agile Web Development with Rails" but now I am getting
strange error (looks to me like some kind of routing problem) under the
passenger/apache. It is working fine under webrick.

The error is:
500 Internal Server Error
The server encountered an internal error or misconfiguration and was
unable to complete your request.

From the log:

SQL (0.3ms) SET SQL_AUTO_IS_NULL=0

Processing ****Controller#index (for 192.168.1.101 at 2010-04-26
18:56:24) [GET]
User Columns (1.7ms) SHOW FIELDS FROM `users`
User Load (0.7ms) SELECT * FROM `users` WHERE
(`users`.`id` IS NULL) LIMIT 1
Redirected to https://******/login/login_page
Filter chain halted as [:authorize] rendered_or_redirected.
Completed in 31ms (DB: 3) | 302 Found [https://******/]

Does anybody have any suggestion what I could do to fix this?

--------------------------------------------
class LoginController < ApplicationController

def add_user
@user = User.new(params[:user])
if request.post? and @user.save
flash.now[:notice] = "User #{@user.name} created"
@user = User.new
end
end

def login_page
session[:user_id] = nil
if request.post?
user = User.authenticate(params[:name], params[:password])
if user
session[:user_id] = user.id
session[:user_name] = user.name
uri = session[:original_uri]
session[:original_uri] = nil
redirect_to(uri || { :action => "index" })
else
flash[:notice] = "Invalid user/password combination"
end
end
end

def logout
session[:user_id] = nil
flash[:notice] = "Logged out"
redirect_to(:action => "login_page")
end

def index
end

def delete_user
if request.post?
user = User.find(params[:id])
if User.count == 1
flash[:notice] = "You can''t remove last remaining user!"
else
user.destroy
end
end
redirect_to(:action => :list_users)
end

def list_users
@all_users = User.find(:all)
end
end
------------------------------------------

class ApplicationController < ActionController::Base
before_filter :authorize, :except => :login_page
helper :all # include all helpers, all the time
protect_from_forgery # See ActionController::RequestForgeryProtection
for details
def create_default_variables(ctrl_name)
session[:ctrl_name] = ctrl_name
end
# Scrub sensitive parameters from your log
# filter_parameter_logging :password
private
def authorize
unless User.find_by_id(session[:user_id])
session[:original_uri] = request.request_uri
flash[:notice] = "Please log in"
redirect_to(:controller => "login", :action => "login_page")
end
end
end
------------------------------------------------
require ''digest/sha1''

class User < ActiveRecord::Base
validates_presence_of :name
validates_uniqueness_of :name
attr_accessor :password_confirmation
validates_confirmation_of :password

def validate
errors.add_to_base("Missing password") if hashed_password.blank?
end

def self.authenticate(name, password)
user = self.find_by_name(name)
if user
expected_password = encrypted_password(password, user.salt)
if user.hashed_password != expected_password
user = nil
end
end
user
end

# ''password'' is a virtual attribute
def password
@password
end
def password=(pwd)
@password = pwd
create_new_salt
self.hashed_password = User.encrypted_password(self.password,
self.salt)
end

def after_destroy
if User.count.zero?
raise "Can''t delete last user"
end
end

private
def self.encrypted_password(password, salt)
string_to_hash = password + "wibble" + salt # ''wibble'' makes it
harder to guess
Digest::SHA1.hexdigest(string_to_hash)
end

def create_new_salt
self.salt = self.object_id.to_s + rand.to_s
end
end
-----------------------------------------
login_page.html.erb

<div class="user-form">
<fieldset>
<legend>Please Log In</legend>
<% form_tag do %>
<p>
<label for="name">Name:</label>
<%= text_field_tag :name, params[:name] %>
</p>
<p>
<label for="password">Password:</label>
<%= password_field_tag :password, params[:password] %>
</p>
<p><%= submit_tag "Login" %></p>
<% end %>
</fieldset>
</div>
-------------------------------------------
routes.rb

ActionController::Routing::Routes.draw do |map|

map.resources :controller1
map.resources :controller2
...

map.root :controller => "controller1"

map.connect '':controller/:action/:id''
map.connect '':controller/:action/:id.:format''
end

-------------------------------------------
and apache log:

[error] [client 192.168.1.101] Request exceeded the limit of 10 internal
redirects due to probable configuration error. Use
''LimitInternalRecursion'' to increase the limit if necessary. Use
''LogLevel debug'' to get a backtrace.

"LogLevel debug" didn''t produce any more information.

Update:


It is definitely routing problem.
I could partially solve the problem by adding this:

map.connect ''login'', :controller => ''login'', :action => "login_page"
map.connect ''login/list_users'', :controller => "login", :action => "list_users"
map.connect ''login/add_user'', :controller => "login", :action => "add_user"
map.connect ''login/logout'', :controller => "login", :action => "logout"

to the routes.rb and just first line is working.
So redirecting to the <app>/login or typing it directly gives login_page action from the login controller.
The remaining problem is that I still can''t use any action directly like /login/list_users or even login/login_page.

推荐答案

您是否在登录后检查了会话变量以确保它们准确保存?如果它们不是,它可能会在原始uri和登录之间来回重定向。
Have you checked your session variables after login to make sure they are saving accurately? If they aren''t, it could be redirecting you back and forth between the original uri and login.


感谢您的回复! :)

你能否更具体地说明我应该检查会话变量的位置。

为什么这个问题不会出现在像
map.connect''login'',: controller => ''login'',:action => login_page

我已经检查过:

map.add_user''add_user'',: controller => " login",:action => " add_user"

with

<%= link_to''添加用户'',add_user_path%>

这与get一起使用但不是post。
Thanks for the reply! :)
Could you please be more specific about where I should check session variables.
And why doesn''t this problem appear with explicit map.connect routes like
map.connect ''login'', :controller => ''login'', :action => "login_page"
I''ve checked for instance with:
map.add_user ''add_user'', :controller => "login", :action => "add_user"
with
<%= link_to ''Add user'', add_user_path %>
This is working with get but not with post.


我会在login_page中查看。
I would check within login_page.
展开 | 选择 | Wrap | 行号


这篇关于在webrick下工作但不在乘客下工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆