2014-09-06 68 views
1

我正在关注rails中的教程和beggienr,在access_controller.rb中 它给了我错误:路由错误 没有路由匹配[POST]“/ access/login”!Rails 4,attemt_login失败

def attempt_login 

    if params[:username].present? && params[:password].present? 
    found_user = AdminUser.where(:username => params[:username]).first 
if found_user 
    authorized_user = found_user.authenticate(params[:password]) 
    end 
end 
if authorized_user 
    # TODO: mark user as logged in 
    flash[:notice] = "You are now logged in." 
    redirect_to(:action => 'index') 

else 
    flash[:notice] = "Invalid username/password combination." 
    redirect_to(:action => 'login') 
end 
end 

login.html.erb

<% @page_title = "Admin Login" %> 

    <div class="login"> 
<%= form_tag access_path do %> 
    <table> 
    <tr> 
    <td><%= label_tag :username %></td> 
    <td><%= text_field_tag :username %></td> 
    </tr> 
    <tr> 
    <td><%= label_tag :password %></td> 
    <td><%= password_field_tag :password %></td> 
</tr> 
<tr> 
    <td>&nbsp;</td> 
    <td><%= submit_tag("Log In") %></td> 
</tr> 
</table> 
    <% end %> 
    </div> 

这里是我的路,我觉得问题应该是在这里,但我不知道如何解决它 的routes.rb

get 'login', to: 'access#create' 
    get 'access/login', to: 'access#create' 

    resources :access do 
    member do 
    get "login" 
    post "login" 
    get "logout" 
    post "logout" 
    get 'attempt_login' 
    end 
end 

回答

1

错误是自我解释,您没有匹配[POST]“/ access/login”的路由。在你的routes.rb添加这条路线:

post "/access/login" => your_controller_name#your_method_name 

更新:

您正在使用GET动词为您的路线,但你的形式作出POST要求,因此航线找不到错误 。你的路线改成这样:

get 'access/login', to: 'access#create', as: "access" 

你的路线改成这样:

post 'access/login', to: 'access#create', as: "access" 

    resources :access do 
    member do 
     get "login" 
     post "login" 
     get "logout" 
     post "logout" 
     get 'attempt_login' 
    end 
    end 

而且你在创建方法,你就必须改变

redirect_to(:action => 'login') 

redirect_to access_path 
+0

谢谢,这个问题解决了,现在给我另一个路线ing错误,:没有路由匹配{:action =>“login”,:controller =>“access”}。 – Amir 2014-09-06 17:49:35

+0

@Amir更新了我的答案。我也没有看到使用职位成员路线的目的 – Mandeep 2014-09-06 18:07:51

+0

@ Mandeeo,我将其更改为您的代码,并且我得到了另一个错误:ActionController :: UrlGenerationError在AccessController中#create 没有路由匹配{:action =>“登录“,:controller =>”access“} – Amir 2014-09-06 18:31:46