本文介绍了Ruby on rails“没有路由匹配"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Rails 的新手,只是在实现一些基本的应用程序.刚刚开始使用我的第二个应用程序并遇到了一个基本问题,但 Google 没有给我任何结果.

出现此错误:

没有路由匹配 {:controller=>"user", :action=>"admin_login"}

这是我的routes.rb

废话::Application.routes.draw 做资源 :items, :cart, :user结尾

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

<头><title>废话</title><%= stylesheet_link_tag :all %><%= javascript_include_tag :defaults %><%= csrf_meta_tag %><身体><div id="headerHolder"><div id="title">等等</div><div id="菜单"><div class="menuItem">博客</div><div class="menuItem"><%= link_to "products", :controller =>项目",:动作=>索引"%>

<div class="menuItem">contact</div><div class="menuItem"><%= link_to "cart", :controller =>大车",:动作=>索引"%>

<div class="menuItem"><%= link_to_unless_current "admin", :controller =>用户",:动作=>"admin_login" %>

<div id="内容"><%=产率%>

</html>

这是我的 user_controller.rb

class UserController :项目结尾结尾

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

解决方案

需要在路由中添加admin_login方法,比如:-

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

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.

Getting this error:

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

Here is my 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'.

<!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

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.

解决方案

You need to add admin_login method to routes, like:-

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

这篇关于Ruby on rails“没有路由匹配"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 18:12