2012-04-21 111 views
0

首先發布在SO上。我對Ruby on Rails比較陌生,我一直在閱讀Michael Hartl的書「Ruby on Rails教程 - 通過示例學習」。但是,在運行我的應用程序時,我會遇到以下問題,我會親切地解決問題。Ruby on Rails - Heroku問題&用戶登錄

1)當嘗試運行我在「生產」模式的應用程序,如下我已經更新了文件「配置/ environments.rb」:

# force Rails into production mode when 
    # you don't control web/app server and can't set it the proper way 
    ENV['RAILS_ENV'] ||= 'production' 

然而,當我運行應用程序,我可以仍然可以看到目前從文件「應用程序/視圖/佈局/ application.html.erb」

<!-- Debug applies only to 'development' environment --> 
      <%= debug(params) if Rails.env.development? -%> 
    <!-- as determined by "if Rails.env.development?" --> 

這使我相信,我仍然以開發模式運行應用程序的調試工具。

2)對於那些已經詢問過有關signin_path問題的人,我仍然無法看到一個解決方案來解決它。我可以註冊用戶,然後自動將他們重定向到他們的個人資料空間。這個是最歡迎的

def current_user # GET current_user 
     @current_user ||= user_from_remember_token 
    end 

    def signed_in? 
     !self.current_user.nil? 
    end 
    . 
    . 
    . 
    private 

     def user_from_remember_token 
      User.authenticate_with_salt(*remember_token) 
     end 

     def remember_token 
      cookies.signed[:remember_token] || [nil, nil] 
     end 

任何幫助:然而,導航菜單不發生相應的變化:

<nav class="round"> 
    <ul> 
     <li><%= link_to "Home", root_path -%></li> 
     <li><%= link_to "Support", support_path -%></li> 
     <% if signed_in? %> 
     <li><%= link_to "Users", users_path -%></li> 
     <li><%= link_to "Profile", current_user -%></li> 
     <li><%= link_to "Settings", edit_user_path(current_user) -%></li> 
     <li><%= link_to "Sign out", signout_path, :method => :delete -%></li> 
     <% else %> 
     <li><%= link_to "Sign in", signin_path -%></li> 
     <% end %> 

這裏是從「應用程序/傭工/ sessions_helper.rb」文件中的代碼。我目前正在嘗試託管我的應用程序Heroku,但沒有得到我不幸需要的支持。

乾杯。

回答

0

如果問題與heroku服務器有關,請檢查link。如果你想在本地運行,rails s -p3001 -e production可能會工作

+0

這有助於縮小問題的範圍,以一個500錯誤,就像我住的應用程序,從而使文件的「公共/ 500.html」被激活(當我從本地運行127.0.0.1:3001而不是端口3000)。 – carmat 2012-04-22 03:31:07

0

看起來像你的signed_in?幫手不會返回你想要的。那麼首先調試一下signed_in?像這樣回覆:

<%= signed_in? %> 

或者你可以用signed_in引發錯誤?作爲消息。

此外它似乎忘記了current_user setter方法,應在創建會話後調用它。你需要有三個方法類似這樣:

def current_user 
    @current_user ||= User.find_by_id(session[:user_id]) 
    end 

    def user_signed_in? 
    !!current_user 
    end 

    def current_user=(user) 
    @current_user = user 
    session[:user_id] = if @current_user ? current_user.id : nil 
    end 

,我建議你提出這個方法的ApplicationController爲受保護的方法。

而最後一個忠告:

!self.current_user.nil? 

看起來非常糟糕。儘量避免使用砰!這應該爲你工作:

self.current_user 
+0

嗨,我試過調試提示,它返回一個空白字符串。我的代碼:'

signed_in?返回:「<%= signed_in? - %>」

'生成'

signed_in?返回:「」

' – carmat 2012-04-22 03:47:55