0

您好我想用 Ruby on Rails 创建一个身份验证。我没有错误 - 消息但我无法登录。

在我的本地主机上出现这张图片(抱歉我不能发布屏幕)


我的工作时间

2011 年 | 登录


登录 - 按钮不起作用。

这是我的代码:

应用程序.html.erb

<!DOCTYPE html>
<html>
<head>
  <title>Mytimes</title>
  <%= stylesheet_link_tag    "application", :media => "all" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>
</head>
<body>
<div id="container">
    <div id="header">
        <h1> My Workingtimes </h1>
    </div>
    <div id="content">
        <% if flash[:notice] %>
        <p id="notice">
            <%= flash[:notice] %>
        </p>
        <% end %>

        <% if flash[:alert] %>
        <p id= "alert">
            <%= flash[:alert] %> 
        </p>
        <% end %>
    </div>
    <div id="footer">
        &copy; 2012 |
        <% if user_signed_in? %>
        <%= link_to "logout", logout_path, method: :delete %> 
        <% else %>
        <%= link_to "login", login_path %>
        <% end %> 
    </div>
</div>
</body>
</html>

路线.rb

Mytimes::Application.routes.draw do
  resources :mytimes
  resources :users, :only => [:new, :create]
  resources :sessions, :only => [:create]

  get "login" => "sessions#new", as: "login"
  post "sessions" => "sessions#create", as: "sessions"
  delete "logout" => "sessions#destroy", as: "logout"

application_controller.rb

类 ApplicationController < ActionController::Baseprotect_from_forgery

private

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

  def user_signed_in?
    current_user.present?
  end




  helper_method :user_signed_in?
end

session_controller.rb

class SessionsController < ApplicationController
  def new
end

  def create
    user = User.find_by_email(params[:email])
    if user && user.authenticate(params[:password])
        session[:user_id] = user.id
        redirect_to mytimes_path,
          notice: "Your are signed!"
    else
      flash.now.alert = "Failed Email or Password!"

      render "new"
    end
end

  def destroy
    session[:user_id] = nil
    redirect_to mytimes_path,
      notice: "You are logged out!"
end

end

会话/new.html.erb

<h2> Log In </h2>

<%= form_tag sessions_path do %>
    <p> 
        <%= label_tag :email %>
        <%= text_field_tag :email, params[:email] %>
    </p>
    <p> 
        <%= label_tag :password %>
        <%= password_field_tag :password %>
    </p>
    <p><%= submit_tag "Log In" %></p>

<% end %>

我很感谢你的帮助。

用户/new.html.erb

<h2>New User</h2>
<%= form_for @user do |f| %>
    <% if @user.errors.any? %>
        <ul>
            <% @user.errors.full_messages.each do |msg| %>
                <li><%= msg %></li>
            <% end %>
        </ul>
    <% end %>

<p>
    <%= f.label :email %>
    <%= f.text_field :email %>
</p>
<p>
    <%= f.label :password %>
    <%= f.password_field :password %>
</p>
<p>
    <%= f.label :password_confirmation %>
    <%= f.password_field :password_confirmation %>
</p>
<p><%= f.submit %></p> 
<% end %>
4

1 回答 1

0

yield你的application.html.erb. 因此不会显示视图。

于 2012-05-07T10:21:57.890 回答