没有路由匹配 [GET] "/users/country.html"

No route matches [GET] "/users/country.html"

直接打开'http://localhost:3000/country.html' or click 'Country' button on navigation bar. But if I click 'Login'/'Signup'(http://localhost:3000/users/signin.html) first and then click 'Country' button, the url link to 'http://localhost:3000/users/country.html'就好了。它显示 'No Route matches [GET] "/users/maison.html"'。更重要的是,'Login' 的颜色变成了一种奇怪的红色,类似于 public 中默认的红色“404”页面。 谁能告诉我为什么不同的点击顺序会导致不同的网址,我该如何解决这个问题?谢谢!

这是一个基于 Ruby on Rail 的应用程序,具有 ruby-2.3.7、'rails'、'~> 5.2.2' 和 mysql.我正在使用 macOS Mojave。

'''
    'application.html.erb':

<div class="outside_container">
  <div class="main_container">
    <div class="navbar clearfix">
      <div class="container">
        <ul class="nav left">
          <li><a href="./introduction.html">Introduction</a></li>
          <li><a href="./country.html">Country</a></li>
          <li><a href="./maison.html">Maison</a></li>
        </ul>


          <% if notice %>
            <p class="notification"><%= notice %></p>
          <% end %>
          <% if alert %>
            <p class="notification"><%= alert %></p>
          <% end %>


      <ul>
          <p class="nav right">
            <% if user_signed_in? %>
              <%= link_to "Edit profile", edit_user_registration_path %> | 
              <%= link_to "Logout", destroy_user_session_path, method: :delete  %> |

            <% else %>
              <%= link_to "Sign up", new_user_registration_path %> |
              <%= link_to "Login", new_user_session_path %> |

            <% end %>
          </p>
        </ul>

    </div>  
  </div>
'''

抱歉遗漏了 route.rb,这里是: 'route.rb':

Rails.application.routes.draw do

  devise_for :users


#make sure devise/session/new, which means the login page is the root page
  devise_scope :user do
  authenticated :user do
    root 'country#index', as: :authenticated_root
  end

  unauthenticated do
    root 'devise/sessions#new', as: :unauthenticated_root
  end
end

get '/country', to: 'country#index'
get '/maison', to: 'maison#index'

  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"

  #root 'page#welcome'

  resources :country
  resources :maison
  resources :introduction
  resources :dashboard

end

所以看起来你有一个指向 /maison 的 href <li><a href="./maison.html">Maison</a></li> 这意味着你的 routes.rb 中的某个地方也需要定义一个与之匹配的路由作为路由接口的控制器:

get '/maison', to: 'home#index`

在上面的路由中,我们说“对 /maison 地址的任何 get 请求都应该触发 home_controller 文件中的 index 方法 Rails.

正确设置路由和控制器后,这应该可以正常工作。

从您的网址中删除圆点。点使其成为相对路径,因此无论您在应用程序中的哪个位置,它都会使用该路径作为这些链接的起点。如果您删除句点,它将始终使用您的根路径来构建链接。

<li><a href="/introduction.html">Introduction</a></li>
<li><a href="/country.html">Country</a></li>
<li><a href="/maison.html">Maison</a></li>

有几件事:

  1. 文件的标题应为 routes.rb(复数) 所以将 route.rb 更改为 routes.rb.

  2. 使用命令$rails routes$rake routes 获取您当前可用的所有路线的列表。如果没有显示,则 rails 没有识别您的路线。

  3. 例如使用“Link至” <%= link_to user.title, users_path(user) %>