简体   繁体   English

Ruby rails 不创建实例变量

[英]Ruby rails not creating instance variables

I am using a lynda.com tutorial for Ruby on Rails.我正在使用 lynda.com 的 Ruby on Rails 教程。 The very first instance of creating and using array instance variables does not work.创建和使用数组实例变量的第一个实例不起作用。 At first I thought it was that @array might have become a reserve word in the newer 5.0 version of rails that I am using, but changing it did not cause the "nil" (undefined) error to go away.起初,我认为 @array 可能已成为我正在使用的较新 5.0 版 rails 中的保留字,但更改它并没有导致“nil”(未定义)错误消失。

What is wrong with Ruby Rails 5.0? Ruby Rails 5.0 有什么问题? It is refusing to define instance variables and to pass them to the appropriate template.它拒绝定义实例变量并将它们传递给适当的模板。

This is extremely aggravating, since rails is not behaving as documented (ie RAILS IS BRAIN DEAD OUT OF THE BOX).这非常令人恼火,因为 Rails 的行为与记录不符(即,RAILS 是开箱即用的大脑)。

****************
demo_controller.rb

class DemoController < ApplicationController

  def index
    render('hello')
  end

  def hello
     @zarray = [1,2,3,4,5]  <------------ this is defined
  end

  def other_hello
    render(:text => "Hello EVERYONE!")
  end

end

******************
hello.html.erb

<h1>Demo#hello</h1>

<p>Hello World!</p>

<%= 1 + 1 %>   <------ works
<% target = "world" %>
</br>
<%= "Hello #{target}" %>   <----- works
</br>
<% @zarray.each do |n| %>   <---- line 10.  Rails claims that @zarray is 
not defined
  <%= n %></br>
<% end %>

ActionView::Template::Error (undefined method `each' for nil:NilClass): ActionView::Template::Error(nil:NilClass 的未定义方法 `each'):

I copied and ran your code and it worked fine.我复制并运行了你的代码,它运行良好。 I guess it could be down to the fact that controllers should as a rails convention be pluralized, and your controller is called DemoController and perhaps you've called demo S #action somewhere?我想这可能是因为控制器应该作为一个 rails 约定被复数化,并且您的控制器被称为DemoController并且也许您已经在某处调用了 demo S #action ? So, generate a new controller from your terminal called:因此,从您的终端生成一个名为的新控制器:

DemosController

with the generator:与发电机:

rails g controller Demos

And copy paste everything from the old controller to the new controller.并将旧控制器中的所有内容复制粘贴到新控制器。 And in your routes.rb you need to first make sure you have the correct resources :demos (the name of your model) which will give you the standard RESTful resources ( index , new , create , etc), but as your ' hello ' method is not a part of the RESTful api, you need to create a custom route for that:在您的 routes.rb 中,您需要首先确保您拥有正确的resources :demos (您的模型的名称),它将为您提供标准的 RESTful 资源( indexnewcreate等),但作为您的“ hello ”方法不是 RESTful api 的一部分,您需要为此创建自定义路由:

  get 'hello' => 'demos#hello',  :as => :hello

get = HTTP method get = HTTP 方法

' hello ' = The URL you want to hello.html.erb to be reachable on: localhost:3000/hello ' hello ' = 您希望 hello.html.erb 可以访问的 URL: localhost:3000/hello

' demos#hello ' = demos is the DemosController and #hello is the action in the controller. demos#hello ” =演示是DemosController#hello是控制器的动作。

' :as => :hello ' (_path)is the named helper you can call in a link_to on any page for instance: link_to hello_path. ' :as => :hello ' (_path) 是您可以在任何页面上的 link_to 中调用的命名助手,例如:link_to hello_path。

I am new to Ruby and Rails.我是 Ruby 和 Rails 的新手。 I tested using a plain STRING instance variable, and THAT got passed from my controller to my template, so the issue was the array definition.我使用一个普通的 STRING 实例变量进行了测试,并且它从我的控制器传递到了我的模板,所以问题是数组定义。 Apparently the older form of array definition that was in the tutorial that I was using no longer works in the version of rails and ruby that I am using.显然,我使用的教程中旧形式的数组定义不再适用于我正在使用的 rails 和 ruby​​ 版本。

I am using Ruby on Rails 5.0.1 with Ruby 2.2.4p230 (2015-12-16 revision 53155) [i386-mingw32] on Windows (rails/ruby compatibility is kosher).我在 Windows 上使用 Ruby on Rails 5.0.1 和 Ruby 2.2.4p230(2015-12-16 修订版 53155)[i386-mingw32](rails/ruby 兼容性是 kosher)。

@zarray = [1,2,3,4,5]     <-- the array is not defined when passed to template
@zarray = Array.new << 1 << 2 << 3 << 4 << 5      <-- this works

It is a bit distressing that Ruby does not even BOTHER to complain about the format of the array definition. Ruby 甚至懒得抱怨数组定义的格式,这有点令人沮丧。 It just DOES NOTHING (ie, refuses to initialize the array).它什么都不做(即拒绝初始化数组)。

In the template:在模板中:

<% @zarray.each do |n| %>
  <%= n %></br>
<% end %>

I should add that @zarray = {1,2,3,4,5] works if you use it after @zarray = Array.new如果你在 @zarray = Array.new 之后使用它,我应该补充说 @zarray = {1,2,3,4,5] 工作

In other words, at some point in the evolution of Ruby, EXPLICITLY classing arrays was introduced?换句话说,在 Ruby 进化的某个时刻,明确地对数组进行了分类? It is not always clear in the documentation.文档中并不总是很清楚。 The earlier tutorial I was using does NOT explicitly class the array.我使用的早期教程没有明确地对数组进行分类。


The problem seems to be in the current RAILS version (5.0.1) that I am running.问题似乎出在我正在运行的当前 RAILS 版本 (5.0.1) 中。 I has done SOMETHING to destroy the ability of Ruby to create an array using array literal syntax.我已经做了一些事情来破坏 Ruby 使用数组文字语法创建数组的能力。 When I run array definitions with literal syntax in IRB, I have no problem creating them.当我在 IRB 中使用文字语法运行数组定义时,创建它们没有问题。 So Ruby is fine.所以Ruby没问题。 It is a serious BUG in the 5.0.1 version of rails.是rails 5.0.1版本的严重BUG。 So I think I should report it.所以我觉得我应该举报。

Welcome to Git (version 1.8.1.2-preview20130201)
$ irb
irb(main):002:0> service_mileage = [5000,15000,30000,60000,100000]
=> [5000, 15000, 30000, 60000, 100000]
irb(main):003:0> service_mileage[0]
=> 5000
irb(main):004:0> service_mileage[2]
=> 30000
irb(main):005:0>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM