Ruby on rails“没有路线匹配”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3757380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:14:20  来源:igfitidea点击:

Ruby on rails "No route matches"

ruby-on-railsruby-on-rails-3

提问by Chris

I am new to Rails and am just implementing some basic applications. Just starting on my second app and have run into what is a basic problem, but Google is yielding me nothing.

我是 Rails 的新手,只是在实现一些基本的应用程序。刚刚开始我的第二个应用程序并遇到了一个基本问题,但谷歌没有给我带来任何好处。

Getting this error:

收到此错误:

No route matches {:controller=>"user", :action=>"admin_login"}

Here is my routes.rb

这是我的routes.rb

Blah::Application.routes.draw do
  resources :items, :cart, :user
end

Here is my applications.html.erb(the idea is this is a header of course, and I'm trying to create a link to 'login'. Right now it's just supposed to set the 'login' session variable to '1'.

这是我的applications.html.erb(当然,这是一个标题,我正在尝试创建一个指向“登录”的链接。现在它只是应该将“登录”会话变量设置为“1”。

<!DOCTYPE html>
<html>
<head>
  <title>Blah</title>
  <%= stylesheet_link_tag :all %>
  <%= javascript_include_tag :defaults %>
  <%= csrf_meta_tag %>
</head>
<body>

<div id="headerHolder">
    <div id="title">blah</div>
    <div id="menu">
        <div class ="menuItem">blog</div>
        <div class ="menuItem">
            <%= link_to "products",  :controller => "items", 
                                     :action => "index" %>
        </div>
        <div class ="menuItem">contact</div>    
        <div class="menuItem">
            <%= link_to "cart",  :controller => "cart", 
                                 :action => "index" %>
        </div>
        <div class="menuItem">
                <%= link_to_unless_current "admin", :controller => "user", 
                                                    :action => "admin_login" %>
        </div>
    </div>
</div>

<div id="content">
    <%= yield %>
</div>

</body>
</html>

And this is my user_controller.rb

这是我的 user_controller.rb

class UserController < ApplicationController

  def index
  end

  def admin_login
    session[:login] = 1
    session[:cart] = nil
    flash[:notice] = "Admin user successfully logged in, cart reset."
    redirect_to :controller => :items
  end

end

What am i missing in my routes.rb? Or otherwise...am sure it's something daft.

我缺少什么routes.rb?否则......我肯定这是愚蠢的事情。

采纳答案by Anubhaw

You need to add admin_loginmethod to routes, like:-

您需要向admin_login路由添加方法,例如:-

map.connect '/user/admin_login', :controller => 'users', :action => 'admin_login'

回答by Simon Perepelitsa

For Rails > 3 you should use the new routing syntax:

对于 Rails > 3 你应该使用新的路由语法:

resources :items, :cart

resource :user do
  # Route GET /user/admin_login
  get 'admin_login', :on => :collection
end

See Rails guidesfor more information about routing.

有关路由的更多信息,请参阅Rails 指南

回答by stonenotes

find “config/routes.rb” file, edit, Locate the following line:

找到“config/routes.rb”文件,编辑,找到以下行:

# See how all your routes lay out with "rake routes"

In this line add the following line, as follows:

在此行中添加以下行,如下所示:

map.connect '',:controller=>"index",:action=>"index"

回答by user1875926

you can use

您可以使用

match 'admin_login' => 'user#admin_login', :as =>'admin_login'

default method for this call is post u can change method behavior by using

此调用的默认方法是发布您可以通过使用更改方法行为

:via => [:post/:put/:get]

回答by Deepak Kalhan

There is a new method in Rails 3. You can use the following:

Rails 3 中有一个新方法。您可以使用以下方法:

get 'admin_login' => "user#admin_login"