0

1 - 我的控制器是这个,但这不是调用视图,这个名称是 login.html.erb,我不明白因为发生了什么,是为了显示登录的形式。

class FinancesController < ApplicationController
  # GET /finances
  # GET /finances.json
  def login
    @user = User.find_by_name(params[:user])
    if @user
      session[:user_id] = @user.id
      redirect_to :action => 'index'
    else
      redirect_to login_url
    end

  end

  def index
    @finances = Finance.all(:order => "created_at")

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @finances }
    end
  end

2 - 有人可以告诉我使用身份验证方法来验证示例登录用户和密码表单,可以向我展示如何使用此方法,find_by_name_and_password 不起作用。

我感谢答案,很多。

路由.rb

Controle::Application.routes.draw do
  match 'login' => 'finances#login' 
  resources :finances
end
4

1 回答 1

1

检查@user对象。

因为,对于您的代码,如果不可用,它将重定向到您的login方法。@user

login.html.erb要从方法渲染login,不需要任何手动重定向。

于 2012-08-13T04:43:15.940 回答