当我直接打开“ http://localhost:3000/country.html”或单击导航栏上的“国家/地区”按钮时,这很好。但是,如果我先单击“登录” /“注册”(http://localhost:3000/users/signin.html),然后单击“国家”按钮,则该URL链接为“ http://localhost:3000/users/country.html”。它显示“没有符合[GET]“ / users / maison.html”的路线”。更重要的是,“登录”的颜色变为奇怪的红色,类似于公共场合的默认红色“ 404”页面。 谁能告诉我为什么不同的单击顺序将导致不同的URL,如何解决这个问题?谢谢!
这是一个基于Ruby on Rail的应用程序,具有devise,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
答案 0 :(得分:0)
因此,似乎您有一个指向<li><a href="./maison.html">Maison</a></li>
的href /maison
,这意味着您的route.rb中需要定义一个与此匹配的路由以及一个控制器路由接口与:
get '/maison', to: 'home#index`
在上述路由中,我们说“对get
地址的任何/maison
请求都应触发Rails index
文件中的home_controller
方法。
正确设置路由和控制器后,它应该可以正常工作。
答案 1 :(得分:0)
The file should be titled routes.rb (plural)
So change route.rb
to routes.rb
.
Use the command $rails routes
or $rake routes
To get a list of all routes you currently have available to you. If nothing shows, then rails is not recognizing your routes.
Use "Link To" for example
<%= link_to user.title, users_path(user) %>
答案 2 :(得分:-1)
从网址中删除该点。点使其成为相对路径,因此无论您在应用程序中的任何位置,都将使用该路径作为这些链接的起点。如果删除句点,它将始终使用您的根路径来构建链接。
<li><a href="/introduction.html">Introduction</a></li>
<li><a href="/country.html">Country</a></li>
<li><a href="/maison.html">Maison</a></li>