0
在這裏,在登錄視圖我有這樣的代碼:Rails 4:如何在登錄視圖中生成錯誤消息?
<p id="notice"><%= notice %></p>
<h1>Log In</h1>
<%= form_tag({controller: "sessions", action: "create"}) do %>
<% if false %>
<% if errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(errors.count, "error") %> unseccessful:</h2>
<ul>
<% errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<% end %>
<div class="field">
<%= label_tag :username %><br/>
<%= text_field_tag :username, params[:username] %><br/>
</div>
<div class="field">
<%= label_tag :password %><br/>
<%= password_field_tag :password %><br/>
</div>
<div class="actions">
<%= submit_tag "Log In" %>
</div>
<% end %>
,如果我輸入不正確的用戶信息,則應該給從控制器的一些注意事項:
def create
user = User.find_by_username(params[:username])
respond_to do |format|
if user && user.password == params[:password]
session[:user_id] = user.id
format.html { redirect_to root_url, notice: 'Logged In successfully' }
format.json { render :index, status: :created, location: @session }
else
format.html { render :new, notice: "Email or password is invalid" }
format.json { render json: @session.errors, status: :unprocessable_entity }
#flash.now.alert = "Email or password is invalid"
#render "new"
end
end
end
而是它只是重新加載登錄頁面沒有任何通知或錯誤信息。我是Rails的新手,你知道我能做什麼嗎?
是的,我現在有「未定義的方法錯誤」的零:NilClass「錯誤(我改變了用戶到@user,並試圖調用@ user.errors.any?它導致發生錯誤。可以這樣做嗎? 你是對的教程,我需要儘快完成這個項目。在學習的同時:) –
這很難幫助,因爲你的代碼有大量的問題,你沒有閱讀教程正確。我仍然指出了很多問題。所以請仔細閱讀教程。 – Deep
我真的不明白我是否應該創建一個form_for @ user或@ session?當我嘗試在控制器中創建@session = Sessions.new時,它也會出現錯誤。你指的是什麼教程? –