Ruby on Rails - Hartl教程第7章 - 无法为UsersController找到动作'index'

时间:2015-06-23 23:33:00

标签: ruby-on-rails ruby ruby-on-rails-3

我是Ruby on Rails的新手,所以我正在关注Michael Hartl的Ruby on Rails教程,我遇到了一个问题。

在第7章第1节中,你应该显示用户页面,但我遇到了这个错误:

“无法找到UsersController”

的操作'索引'

当我尝试去“localhost:3000 / users”时

我不确定我需要包含的所有信息,所以这里有一些,如果我需要更多,请告诉我。我非常感谢能得到的任何帮助。我确实在这里查找了类似的问题,但没有人帮我解决我的问题。

这是我的routes.rb

Rails.application.routes.draw do
root              'static_pages#home'
  get 'help' =>     'static_pages#help'
  get 'about' =>    'static_pages#about'
  get 'contact' =>  'static_pages#contact'
  get 'signup' =>   'users#new'
resources :users
end

users_controller.rb

class UsersController < ApplicationController

  def show
    @user = User.find(params[:id])
  end

  def new
  end
end

show.html.erb

<%= @user.name %>, <%= @user.email %>

我已经仔细检查了我的代码与教程,我似乎找不到任何区别。我能想到的唯一问题可能就是它要求我创建show.html.erb我刚刚点击并在我的在线IDE中点击了“创建文件”(教程推荐的Cloud 9.io)。 Hartl只是说你必须手工制作它,所以我认为这就是他的意思。再次,任何帮助将不胜感激。谢谢。

2 个答案:

答案 0 :(得分:1)

你只需要一个索引的地方标记,这是在两个文件中完成的。答案在第9章的书中,这就是你所做的。

首先,在users_controller.rb中,添加:

<% provide(:title, 'All users') %>
<h1>All Users</h1>

接下来,创建文件app / views / users / index.html.erb并添加此

<!DOCTYPE html>
<!--[if IE 9]><html lang="en" class="ie9 desktop-view not-mobile-device "><![endif]-->
<!--[if (!IE 9) | (!IE)]><!--><html lang="en" class="desktop-view not-mobile-device "><!--<![endif]-->
  <head>
    <meta charset="utf-8">
    <title>[Tutorial] How to create a modal/pop-up in Webflow - Webflow Forums</title>
    <meta name="description" content="Community for Webflow Users to get Support and Inspiration">
    <meta name="author" content="">
<meta name="generator" content="Discourse 1.4.0.beta3 - https://github.com/discourse/discourse version 18f887772df5b25df33e7fb1bcf14d379bb9c624">

<link href="/testdrive.css?__ws=forum.webflow.com" media="all" rel="stylesheet" />
</head>
<body>
<form action="demo_form.asp">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
  <input type="submit" value="Submit">
</form>
  <div class="w-section section"></div>
  <div class="w-section"></div>
  <div class="w-section section">
    <div class="w-container container-style">
      <div class="label try-it">SIGN-IN</div>
      <div class="w-form">
        <form id="email-form-3" name="email-form-3" data-name="Email Form 3">
          <label for="name-4">Name:</label>
          <input class="w-input" id="name-4" type="text" placeholder="Enter your name" name="name" data-name="Name 4">
          <label for="email-6">Email Address:</label>
          <input class="w-input" id="email-6" type="email" placeholder="Enter your email address" name="email" data-name="Email 6" required="required">
          <input class="w-button" type="submit" value="Submit" data-wait="Please wait...">
        </form>
        <div class="w-form-done">
          <p>Thank you! Your submission has been received!</p>
        </div>
        <div class="w-form-fail">
          <p>Oops! Something went wrong while submitting the form</p>
        </div>
      </div>
    </div>
    <div class="w-container container-style">
      <div class="w-form">
        <form id="email-form-4" name="email-form-4" data-name="Email Form 4">
          <label for="name-5">Name:</label>
          <label for="email-7">Email Address:</label>
          <input class="w-input" id="email-7" type="email" placeholder="Enter your email address" name="email" data-name="Email 7" required="required">
          <input class="w-button" type="submit" value="Submit" data-wait="Please wait...">
        </form>
        <div class="w-form-done">
          <p>Thank you! Your submission has been received!</p>
        </div>
        <div class="w-form-fail">
          <p>Oops! Something went wrong while submitting the form</p>
        </div>
      </div>
      <div class="label try-it">SIGN-IN</div>
    </div>
    <div class="w-container container-style">
      <div class="label try-it">SIGN-IN</div>
      <div class="w-form">
        <form id="email-form-5" name="email-form-5" data-name="Email Form 5">
          <label for="name-5">LOCATION</label>
          <input class="w-input" id="name-5" type="text" placeholder="Enter your name" name="name" data-name="Name 5">
          <label for="field">TIME</label>
          <input class="w-input" id="field" type="text" placeholder="Example Text" name="field" required="required">
          <label for="email-8">INVITEES</label>
          <input class="w-input" id="email-8" type="email" placeholder="Enter your email address" name="email" data-name="Email 8" required="required">
          <input class="w-button" type="submit" value="Submit" data-wait="Please wait...">
        </form>
        <div class="w-form-done">
          <p>Thank you! Your submission has been received!</p>
        </div>
        <div class="w-form-fail">
          <p>Oops! Something went wrong while submitting the form</p>
        </div>
      </div>
    </div>
    <div class="w-container container-style">
      <div class="label try-it">SIGN-IN</div><a class="button" href="#">LIST USERS</a>
    </div>
    <div class="w-container container-style">
      <div class="label try-it">SIGN-IN</div><a class="button" href="#">LIST EVENTS</a>
    </div>
  </div>
  <a class="w-inline-block" href="#">
    <a class="w-lightbox w-inline-block" href="#"><img class="close-modal" src="https://d3e54v103j8qbb.cloudfront.net/img/placeholder-thumb.svg">
      <script type="application/json" class="w-json">
        { "items": [] }
      </script>
    </a>
  </a>
  <div class="modal-window">
    <div class="w-widget w-widget-map" data-widget-latlng="51.511214,-0.119824" data-widget-style="roadmap" data-widget-zoom="12"></div>
  </div>
  <div class="w-section section"></div>
  <label>Label</label>
  <div class="w-section modal-background"></div>
</body>

本书后面会有更多内容,但为了继续没有任何愚蠢的,不必要的错误,这就足够了。

答案 1 :(得分:0)

您需要向控制器添加索引操作。

class UsersController < ApplicationController

  def show
    @user = User.find(params[:id])
  end

  def new
  end

  def index
    @users = User.all
  end
end