尝试在Ruby on Rails中创建新项目时,无法获得表单

时间:2015-12-08 17:49:23

标签: ruby-on-rails ruby ruby-on-rails-4

我刚刚开始自己​​学习Ruby on Rails,我的主要学习来源是来自这里的在线网站和codecademy,它只能很好地解释当我编码时发生的事情。

我的项目是一个非常简单的IT库存站点,它显示一个包含项目信息的数据库。这是用CRUD练习的,现在我被困在创建部分。每当我点击主页面上的链接时,都会想到一个页面,上面有一个表格可以输入然后提交,但它甚至不会显示带有表格的页面。这是我目前的代码。

的routes.rb

Array (432)
0{hour: 1, value: 0, key: "Air Condition/Ventilation Equipment"}
1{hour: 2, value: 0, key: "Air Condition/Ventilation Equipment"}
2{hour: 3, value: 0, key: "Air Condition/Ventilation Equipment"}
3{hour: 4, value: 0, key: "Air Condition/Ventilation Equipment"}
4{hour: 5, value: 0, key: "Air Condition/Ventilation Equipment"}
5{hour: 6, value: 0, key: "Air Condition/Ventilation Equipment"}
6{hour: 7, value: 0, key: "Air Condition/Ventilation Equipment"}
7{hour: 8, value: 0, key: "Air Condition/Ventilation Equipment"}
8{hour: 9, value: 0, key: "Air Condition/Ventilation Equipment"}
9{hour: 10, value: 0, key: "Air Condition/Ventilation Equipment"}
10{hour: 11, value: 0, key: "Air Condition/Ventilation Equipment"}
11{hour: 12, value: 0, key: "Air Condition/Ventilation Equipment"}
12{hour: 13, value: 0, key: "Air Condition/Ventilation Equipment"}
13{hour: 14, value: 0, key: "Air Condition/Ventilation Equipment"}
14{hour: 15, value: 0, key: "Air Condition/Ventilation Equipment"}
15{hour: 16, value: 0, key: "Air Condition/Ventilation Equipment"}
16{hour: 17, value: 0, key: "Air Condition/Ventilation Equipment"}
17{hour: 18, value: 0, key: "Air Condition/Ventilation Equipment"}
18{hour: 19, value: 0, key: "Air Condition/Ventilation Equipment"}
19{hour: 20, value: 0, key: "Air Condition/Ventilation Equipment"}
20{hour: 21, value: 0, key: "Air Condition/Ventilation Equipment"}
21{hour: 22, value: 0, key: "Air Condition/Ventilation Equipment"}
22{hour: 23, value: 0, key: "Air Condition/Ventilation Equipment"}
23{hour: 24, value: 0, key: "Air Condition/Ventilation Equipment"}
24{hour: 1, value: 1, key: "Alarms"}
25{hour: 2, value: 0, key: "Alarms"}
26{hour: 3, value: 1, key: "Alarms"}
27{hour: 4, value: 2, key: "Alarms"}
28{hour: 5, value: 0, key: "Alarms"}
29{hour: 6, value: 1, key: "Alarms"}
30{hour: 7, value: 1, key: "Alarms"}
31{hour: 8, value: 3, key: "Alarms"}
32{hour: 9, value: 1, key: "Alarms"}
33{hour: 10, value: 2, key: "Alarms"}
34{hour: 11, value: 1, key: "Alarms"}
35{hour: 12, value: 3, key: "Alarms"}
36{hour: 13, value: 1, key: "Alarms"}
37{hour: 14, value: 1, key: "Alarms"}
38{hour: 15, value: 0, key: "Alarms"}
39{hour: 16, value: 0, key: "Alarms"}
40{hour: 17, value: 2, key: "Alarms"}
41{hour: 18, value: 0, key: "Alarms"}
42{hour: 19, value: 2, key: "Alarms"}
43{hour: 20, value: 1, key: "Alarms"}
44{hour: 21, value: 0, key: "Alarms"}
45{hour: 22, value: 1, key: "Alarms"}
46{hour: 23, value: 1, key: "Alarms"}
47{hour: 24, value: 0, key: "Alarms"}
48{hour: 1, value: 1, key: "Banging/Pounding"}
49{hour: 2, value: 3, key: "Banging/Pounding"}
50{hour: 3, value: 0, key: "Banging/Pounding"}
51{hour: 4, value: 3, key: "Banging/Pounding"}
52{hour: 5, value: 1, key: "Banging/Pounding"}
53{hour: 6, value: 1, key: "Banging/Pounding"}
54{hour: 7, value: 1, key: "Banging/Pounding"}
55{hour: 8, value: 0, key: "Banging/Pounding"}
56{hour: 9, value: 0, key: "Banging/Pounding"}
57{hour: 10, value: 0, key: "Banging/Pounding"}
58{hour: 11, value: 1, key: "Banging/Pounding"}
59{hour: 12, value: 1, key: "Banging/Pounding"}
60{hour: 13, value: 1, key: "Banging/Pounding"}
61{hour: 14, value: 1, key: "Banging/Pounding"}
62{hour: 15, value: 2, key: "Banging/Pounding"}
63{hour: 16, value: 1, key: "Banging/Pounding"}
64{hour: 17, value: 2, key: "Banging/Pounding"}
65{hour: 18, value: 3, key: "Banging/Pounding"}
66{hour: 19, value: 5, key: "Banging/Pounding"}
67{hour: 20, value: 2, key: "Banging/Pounding"}
68{hour: 21, value: 2, key: "Banging/Pounding"}
69{hour: 22, value: 2, key: "Banging/Pounding"}
70{hour: 23, value: 1, key: "Banging/Pounding"}
71{hour: 24, value: 0, key: "Banging/Pounding"}
72{hour: 1, value: 0, key: "Barking Dog"}
73{hour: 2, value: 1, key: "Barking Dog"}
74{hour: 3, value: 0, key: "Barking Dog"}
75{hour: 4, value: 2, key: "Barking Dog"}
76{hour: 5, value: 4, key: "Barking Dog"}
77{hour: 6, value: 3, key: "Barking Dog"}
78{hour: 7, value: 2, key: "Barking Dog"}
79{hour: 8, value: 3, key: "Barking Dog"}
80{hour: 9, value: 1, key: "Barking Dog"}
81{hour: 10, value: 3, key: "Barking Dog"}
82{hour: 11, value: 0, key: "Barking Dog"}
83{hour: 12, value: 1, key: "Barking Dog"}
84{hour: 13, value: 0, key: "Barking Dog"}
85{hour: 14, value: 1, key: "Barking Dog"}
86{hour: 15, value: 3, key: "Barking Dog"}
87{hour: 16, value: 3, key: "Barking Dog"}
88{hour: 17, value: 6, key: "Barking Dog"}
89{hour: 18, value: 1, key: "Barking Dog"}
90{hour: 19, value: 1, key: "Barking Dog"}
91{hour: 20, value: 3, key: "Barking Dog"}
92{hour: 21, value: 2, key: "Barking Dog"}
93{hour: 22, value: 2, key: "Barking Dog"}
94{hour: 23, value: 0, key: "Barking Dog"}
95{hour: 24, value: 0, key: "Barking Dog"}
96{hour: 1, value: 0, key: "Car/Truck Horn"}
97{hour: 2, value: 1, key: "Car/Truck Horn"}
98{hour: 3, value: 1, key: "Car/Truck Horn"}
99{hour: 4, value: 0, key: "Car/Truck Horn"}
...

项目控制器

Rails.application.routes.draw do
get '/items' => 'items#index' 
get 'items/new' => 'items#new'
post 'items' => 'items#create'
root 'items#index'  #homepage and /items are same page for now

new.html.erb

class ItemsController < ApplicationController
def index
    @items = InventoryItem.all
end

def new
    @item = InventoryItem.new
end

def create
    @item = InventoryItem.new(item_params)
    if @item.save
        redirect_to '/items'
    else
        render 'new'
    end
end

private
def item_params
    params.require(:item).permit( :idnumber, :vutag, :fund, :dept, :bldg, :person, :mfct, :mfctdate, :commonname, :modelnum, :serial, :cost)
end
end

的index.html

<%= form_for(@item) do |f| %>  
<div> 
  <%= f.label :inventoryitem %><br>
  <%= f.text_area :idnumber %><br> 
  <%= f.text_area :vutag %><br>
  <%= f.text_area :fund %><br>
  <%= f.text_area :dept %><br>
  <%= f.text_area :bldg %><br>
  <%= f.text_area :person %><br>
  <%= f.text_area :mfct %><br>
  <%= f.text_area :mfctdate %><br>
  <%= f.text_area :commonname %><br>
  <%= f.text_area :modelnum %><br>
  <%= f.text_area :serial %><br>
  <%= f.text_area :cost %><br>
</div> 
<div> 
  <%= f.submit "Create" %> 
</div> 
<% end %>

一个非常基本的应用程序,用于显示数据库中的信息。 (一旦它起作用,我会回去做它。

显示该表的页面工作正常,当我点击链接并转到items / new时它会给我这个错误:

<table>
<tr>
  <th>IDNum</th>
  <th>VU Tag</th>
  <th>Fund</th>
  <th>Dept</th>
  <th>Bldg</th>
  <th>Person</th>
  <th>Mfct</th>
  <th>Mfct Date</th>
  <th>Common Name</th>
  <th>Model</th>
  <th>Serial</th>
  <th>Cost</th>
</tr>
<tr>
  <% @items.each do |item| %>
  <td> <%= item.idnumber %> </td>
  <td> <%= item.vutag %> </td>
  <td> <%= item.fund %> </td>
  <td> <%= item.dept %> </td>
  <td> <%= item.bldg %> </td>
  <td> <%= item.person %> </td>
  <td> <%= item.mfct %> </td>
  <td> <%= item.mfctdate %> </td>
  <td> <%= item.commonname %> </td>
  <td> <%= item.modelnum %> </td>
  <td> <%= item.serial %> </td>
  <td> <%= item.cost %> </td>
  <% end %>
</tr>
</table>
<br><br>
<%= link_to "New Entry", "items/new" %>

任何人都可以帮我弄清楚错误是什么以及我需要修复它的位置吗?我不相信它的html页面本身,而是相反的东西,或者说错误的东西。 Ruby on Rails中的命名约定让我有所收获。

2 个答案:

答案 0 :(得分:0)

在路线中使用resources,它会创建命名路线和路线。

Rails.application.routes.draw do
  resources :items, only:[:index, :new, :create]
  root 'items#index'
end

此外,您应该将模型命名为与控制器和路由相同,以使默认设置起作用

答案 1 :(得分:0)

Rails使用约定优于配置。这意味着名字很重要。您的模型是InventoryItem。因此,您的控制器应该被称为InventoryItemsController。

在您的路线中,正如Vasfed所提到的,惯例是使用资源方法生成必要的路径。因此,而不是get '/items/new'等,只需使用resources :inventory_items

假设您进行了这些更改。在您的InventoryItemsController new操作中,设置@inventory_item = InventoryItem.new。当然,您可以随意调用变量,但将其称为实际变量是有意义的。这就是为什么我建议@inventory_item。好到目前为止?

在表单中,执行以下操作:form_for(@inventory_item) do |f|。 Rails会自动将其转换为该模型实例的“新”路径:new_inventory_item_path(aka inventory_items / new)。