【问题标题】:How can I connect my home page with other pages?如何将我的主页与其他页面连接起来?
【发布时间】:2019-02-03 11:35:27
【问题描述】:

我正在使用 Gem 'Devise' 为我的页面设置用户和管理员。我创建了一个登录页面,要求用户首先登录,然后在成功登录后,我想将用户重定向到模型 Category 的主页,该主页将只显示产品类别列表。然而,我还没有找到正确的方法来做到这一点。这是我的路线文件:

Rails.application.routes.draw do
  resources :categories
  root to: 'pages#home'
  devise_for :users
end

这是我在视图页面中的 home.html.erb 文件:

<h1>Login page</h1>
<% if current_user %>
<%= link_to 'Categories', category_path(@category) %>
<%= link_to 'Sign Out', destroy_user_session_path, method:
:delete %>
<% else %>
<%= link_to 'Sign Up', new_user_registration_path %>
<%= link_to 'Sign In', new_user_session_path %>
<% end %>

我收到一条错误消息:

 ActionView::Template::Error 
(No route matches {:action=>"show",:controller=>"categories", :id=>nil},
 missing required keys: [:id]):

如何将视图Pagehome.html.erb文件与index.html.erb连接起来>类别视图?提前感谢您提供的任何帮助

【问题讨论】:

  • categories_path替换category_path(@category)

标签: ruby-on-rails ruby routes


【解决方案1】:

如何将视图 Page 的 home.html.erb 文件与 Category 视图的 index.html.erb 连接起来?

如果您想要的是查看所有类别的链接,那么为什么要使用帮助程序来查看单个类别category_path(@category)(并传递一个不存在的@category) ?相反,使用这个:

<%= link_to 'Categories', categories_path %>

【讨论】:

  • 我试过这个,但它不起作用。你知道为什么吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-02
  • 2017-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多