我從來沒有開發任何網站,但現在我必須做一個簡單的應用程序來顯示現有數據庫中的一些數據。事實上,每個用戶只能看到他/她的相應數據,所以我需要某種形式的身份驗證來完成加入該用戶和其他表的查詢。我試圖讓事情變得簡單,所有用戶所需要做的就是鍵入它的用戶名,這是佩索阿表上的主鍵(沒有密碼,沒有電子郵件確認,沒有任何內容)。找不到「登錄」的人=
我已經閱讀了一些關於Rails on RailsGuides的教程,以及www.rubypigeon.com/posts/how-to-implement-simple-authentication-without-devise/的認證,但我無法獲得我的登錄工作。
這裏是我到目前爲止的代碼:
的HomeController:
class HomeController < ApplicationController
def index
end
def login
@user = Pessoa.new
end
def sign_in
@user = Pessoa.find(params[:id])
if @user
session.clear
session[:user_id] = @user.id
render '/'
else
@error = 'Username or password was incorrect'
erb :sign_in
end
end
def sign_out
session.clear
render '/'
end
helper_method :current_user
def current_user
if session[:user_id]
Pessoa.find (session[:user_id])
else
nil
end
end
end
的意見/家/ login.html.erb:
<%= form_for @user, url: home_sign_in_path do |f| %>
<div class="field">
<%= f.label :"digite seu login" %>
<%= f.text_field :id %>
</div>
<div class="actions">
<%= f.submit :entrar %>
</div>
<% end %>
的routes.rb
Rails.application.routes.draw do
root 'home#index'
get 'home/index'
get 'home/login', to: 'home#login'
post 'home/sign_in', to: 'home#sign_in'
post 'home/sign_out', to: 'home#sign_out'
resources :pessoas
end
/views/home/index.html.erb有一個大量的靜態HTML的東西,它唯一的Ruby代碼是
<%= link_to 'Login', home_login_path %>
Pessoa.rb
class Pessoa < ApplicationRecord
self.table_name = 'pessoa'
self.primary_key = 'login'
end
我與PostgreSQL的遺留表的工作,這就是爲什麼主鍵,就必須改變。
每當我到localhost:3000/home/login並鍵入我的登錄名時,我會收到this 錯誤消息。奇怪的部分是,如果我運行完全相同的線路,在控制檯上給我提供了錯誤,它就可以正常工作。正如我所說的,我對Rails和一般的Web開發非常陌生,所以我認爲這是一個微不足道的細節,我無法看到。任何幫助,將不勝感激。
順便說一句,不介意資源名稱,「Pessoa」的意思是「葡萄牙人」,這是我的母語。
development.log:
Started GET "/home/login" for 127.0.0.1 at 2017-06-06 20:22:45 -0300
Processing by HomeController#login as HTML
Rendering home/login.html.erb within layouts/application
Rendered home/login.html.erb within layouts/application (12.4ms)
Completed 200 OK in 1504ms (Views: 1369.5ms | ActiveRecord: 0.0ms)
Started POST "/home/sign_in" for 127.0.0.1 at 2017-06-06 20:22:50 -0300
Processing by HomeController#sign_in as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Jzt0Yeven91Kl8DrfjpJa8I9PJjr7SzKmE43lwprjlopV7l+zFU6EUjUgAiKYJhzemm7xu8NGfiPj3E5h5EiYw==", "pessoa"=>{"id"=>"gabrielbrito"}, "commit"=>"entrar"}
[1m[36mPessoa Load (1.5ms)[0m [1m[34mSELECT "pessoa".* FROM "pessoa" WHERE "pessoa"."login" = $1 LIMIT $2[0m [["login", nil], ["LIMIT", 1]]
Completed 404 Not Found in 9ms (ActiveRecord: 1.5ms)
ActiveRecord::RecordNotFound (Couldn't find Pessoa with 'login'=):
app/controllers/home_controller.rb:10:in `sign_in'
Started PUT "/__web_console/repl_sessions/ed3bb60f3a72a0c0b0c951fa03c0f23d" for 127.0.0.1 at 2017-06-06 20:22:56 -0300
Started PUT "/__web_console/repl_sessions/ed3bb60f3a72a0c0b0c951fa03c0f23d" for 127.0.0.1 at 2017-06-06 20:22:58 -0300
Started PUT "/__web_console/repl_sessions/ed3bb60f3a72a0c0b0c951fa03c0f23d" for 127.0.0.1 at 2017-06-06 20:23:04 -0300
[1m[36mPessoa Load (1.2ms)[0m [1m[34mSELECT "pessoa".* FROM "pessoa" WHERE "pessoa"."login" = $1 LIMIT $2[0m [["login", "gabrielbrito"], ["LIMIT", 1]]
Started GET "/home/login" for 127.0.0.1 at 2017-06-06 21:27:45 -0300
Processing by HomeController#login as HTML
Rendering home/login.html.erb within layouts/application
Rendered home/login.html.erb within layouts/application (6.2ms)
Completed 200 OK in 1626ms (Views: 1152.5ms | ActiveRecord: 7.5ms)
Started POST "/home/sign_in" for 127.0.0.1 at 2017-06-06 21:27:52 -0300
Processing by HomeController#sign_in as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"q2ffWM1gqaXfCrsFm7KlGKwR/iTDoQBflquem2BKSaeMyKBiIaUrtnIkoEXNztUWRasIGGg0qZZxpdYao8xb9Q==", "pessoa"=>{"id"=>"gabrielbrito"}, "commit"=>"entrar"}
[1m[36mPessoa Load (2.8ms)[0m [1m[34mSELECT "pessoa".* FROM "pessoa" WHERE "pessoa"."login" = $1 LIMIT $2[0m [["login", "gabrielbrito"], ["LIMIT", 1]]
Completed 500 Internal Server Error in 23ms (ActiveRecord: 2.8ms)
NoMethodError (undefined method `empty?' for nil:NilClass):
app/controllers/home_controller.rb:15:in `sign_in'
編輯:解決了,感謝@格里的幫助。
錯誤和控制器是不同的,也就是說,你的代碼有'@user = Pessoa.find(params [:id])',但錯誤說'@user = Pessoa.find(params [:pessoa] [:id])''。當前的代碼是否也會給你一個錯誤? – Gerry
你能在控制檯/日誌中顯示輸出嗎? – Gerry
我的不好,我試過'@user = Pessoafind(params [:id])',但是在控制檯上輸入的名稱給了我零,所以我將它改爲'@user = Pessoa.find(params [:pessoa] [ :id])',即使控制檯向我顯示錶中的記錄,新代碼也會給我提供相同的錯誤。 編輯:用日誌更新 –