【问题标题】:How to create a link from the welcome page to another ruby page如何创建从欢迎页面到另一个 ruby​​ 页面的链接
【发布时间】:2016-07-14 18:06:28
【问题描述】:

目前,我的导航栏内容如下所示:

<li><%= link_to "Portfolio", projects_path, :class => "header" %></li>
<li><%= link_to "Articles", posts_path, :class => "header" %></li>
<li><%= link_to 'Contact',  contact_path, :class => "header" %></li>

前两个链接可以正常工作,但最后一个链接不行。 我如何引用正确性?联系页面只是一个简单的静态页面(没有被推送的项目或博客)。

这是我的路线.rb

Rails.application.routes.draw do
  resources :contact
  get 'contact/index'
  resources :posts
  resources :projects
  get 'welcome/index'
  root 'welcome#index'
end

这是控制器

class ContactController < ApplicationController
    def index
    end
end

如果我看一下 rake 路线,我会明白:

contact_index GET    /contact(.:format)           contact#index
              POST   /contact(.:format)           contact#create
  new_contact GET    /contact/new(.:format)       contact#new
 edit_contact GET    /contact/:id/edit(.:format)  contact#edit
      contact GET    /contact/:id(.:format)       contact#show
              PATCH  /contact/:id(.:format)       contact#update
              PUT    /contact/:id(.:format)       contact#update
              DELETE /contact/:id(.:format)       contact#destroy
              GET    /contact/index(.:format)     contact#index

我是 ruby​​ 的新手,我期待提供帮助。提前致谢!

【问题讨论】:

    标签: ruby-on-rails ruby blogs contact railsapps


    【解决方案1】:

    将第三行替换为以下行:

    <li><%= link_to 'Contact', contact_index_path, class: "header" %></li>
    

    【讨论】:

      【解决方案2】:

      从您的路由文件中删除 get 'contact/index' 并尝试。

      当您添加 resources :contact 时,您已经有一个索引为 contact 的路径

      【讨论】:

      • 亲爱的 Kumar,很遗憾,这还不够。当我转到本地主机时,出现以下错误:没有路由匹配 {:action=>"show", :controller=>"contact"} missing required keys: [:id]
      • 您的控制器名称是ContactController 还是ContactsController?如果它是复数,你应该改用resources :contacts
      • 是contact_controller.rb
      • 您的联系人控制器中是否只有一种方法? index?
      猜你喜欢
      • 2023-03-28
      • 2020-10-27
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 2010-11-18
      • 2019-09-10
      • 2012-09-04
      • 1970-01-01
      相关资源
      最近更新 更多