Rails Rails 登录例子失败 (已解决,原来是路由适配的问题)

VincentJiang · October 20, 2013 · Last by jxs471494539 replied at October 20, 2013 · 2980 hits

我建了一个登录页面【index.html.erb】:

<%= form_tag do %>
    <fieldset>
    <legend>Please Input Details</legend>
      <div>
        <label for="username">username:</label>
        <%= text_field_tag :username, params[:username] %>
      </div>

      <div>
        <label for="password">password:</label>
        <%= password_field_tag :password, params[:password] %>
      </div>

      <div>
        <%= submit_tag "Login" %>
      </div>
    </fieldset>
<% end %>

用以下后台代码进行登录验证【welcome_controller.erb】:

class WelcomeController < ApplicationController
  def index
    if "Vincent" == params[:username] && "123456" == params[:password]
        redirect_to(welcome_login_path)
    else
        redirect_to(welcome_logout_path)
    end
  end

  def login
  end

  def logout
  end
end

结果只会跳至http://localhost:3000/welcome/logout,请问我的代码哪里出错了?

请先动 index 第一行打印一下 这两个变量有没有传过来……logger.debug

哥子,请问你 form_tag 为啥没有指明任何请求地址??

#2 楼 @small_fish__ 指向“/welcome/index”也不行,我觉得是我 controller 的代码没有写正确吧?但不知道错在哪。。。

#3 楼 @jxs471494539 先看下日志,post 请求具体访问的是哪里

#4 楼 @small_fish__ post 请求指向的就是/welcome/index。 而且不用我按 submit,表单就自动提交的,好奇怪。。。

You need to Sign in before reply, if you don't have an account, please Sign up first.