【问题标题】:Link Button to a page not showing链接按钮到未显示的页面
【发布时间】:2014-08-26 21:04:46
【问题描述】:

我已经从 https://github.com/lockitron/selfstarter 克隆了 repo,我正在尝试在标题中创建指向新页面的链接,但链接没有显示

我想在标题中创建一个“探索”按钮,所以我创建了一个资源管理器控制器和模型运行迁移并在标题部分中使用 link_to 辅助方法来创建链接到页面的新按钮,但它没有显示出来。如何做我显示了我的“探索”按钮,该按钮链接到资源管理器文件夹中的“show.html.erb 页面”

routes.rb

resources :explorer   

explorer_controller

class ExplorerController < ApplicationController
    def show

end
end

show.html.erb

<p> hello </p>

header.html.erb

<div id="header">
 <%= link_to 'Explore, explore_path %>


</div>

我也收到以下错误

ActionController::UrlGenerationError in Preorder#index
Showing /Users/views/preorder/_header.html.erb where line #2 raised:

No route matches {:controller=>"explorer", :action=>"show"} missing required keys: [:id]
Extracted source (around line #2):
1 <div id="header">
2  <%= link_to 'Explore', explorer_path %>
3 <div class="wrapper clearfix">
4 <h1 id="lockitron_header"><a href="/"><%= Settings.product_name %></a></h1>

Trace of template inclusion: views/layouts/application.html.erb

Application Trace | Framework Trace | Full Trace
app/views/preorder/_header.html.erb:2:in `_app_views_preorder__header_html_erb___3778671432332580621_70190937023480'
app/views/layouts/application.html.erb:15:in `_app_views_layouts_application_html_erb___270280154590328587_70190919949800'

【问题讨论】:

  • 这是一个错字还是你真的错过了一个结束 ' in :
  • 另外,你是否在你的应用布局中渲染header.html.erb
  • rb512 我错过了逗号,我添加了它。JTG 我在应用程序布局中呈现 header.html.erb。我仍然看不到按钮,我也因为缺少必需的键而收到错误

标签: html ruby-on-rails ruby


【解决方案1】:

这两个问题的原因相同。但在开始之前,我想指出一些关于 Rails 资源的事情。作为the documentation shows,您可以拥有pluralsingular 资源。

我无法确定您的资源管理器资源需要是单数还是复数,但我们假设您的应用程序中只能有一个资源管理器。这意味着你有一个单一的资源,它在你的routes.rb 中定义如下:

resource :explorer

Rails 现在将假定控制器以复数形式 explorers 命名。打开终端并运行rake routes。您将看到所有可用路径和路由映射到的控制器。

您的控制器将位于app/controllers/explorers_controller.rb,如下所示:

class ExplorersController < ApplicationController
  def show
  end
end

回到你的问题。 _header 部分由于错误而无法呈现:

没有路由匹配 {:controller=>"explorer", :action=>"show"} 缺少必需的键:[:id]

这个错误说明您需要在路径中提供Explorer 记录(或其 ID),以便 Rails 知道要链接到哪个资源管理器。然而,这仅适用于多个资源的情况。

如果是单一资源,则无需提供其 ID,因为只能有一个!因此,如果您像我解释的那样调整了路线和控制器,您的视图将按原样工作。

【讨论】:

  • 我将资源更改为资源,错误消失了
猜你喜欢
  • 2019-12-05
  • 1970-01-01
  • 1970-01-01
  • 2014-01-11
  • 1970-01-01
  • 2014-02-15
  • 2018-12-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多