index.html.erb
<h1>this is show contact us page</h1>
<table>
<tr>
<th>name</th>
<th>email</th>
<th>mobile no</th>
<th>heading</th>
<th>contents</th>
</tr>
<% @contacts.each do |contact| %>
<tr>
<td>
<p>
<strong>Name:</strong>
<%= contact.name %>
</p>
</td>
<td>
<p>
<strong>Email:</strong>
<%= contact.email %>
</p>
</td>
<td>
<p>
<strong>mobile No:</strong>
<%= contact.mob %>
</p>
</td>
<td>
<p>
<strong>head:</strong>
<%= contact.head %>
</p>
</td>
<td>
<p>
<strong>contents:</strong>
<%= contact.content %>
</p>
</td>
</tr>
<% end %>
</table>
contacs_controller.erb
class ContactsController < ApplicationController
def index
@contacts = Contact.all
end
def show #for showing the conatc
@contact = Contact.find(params[:id])
end
def new #to create new contact us
end
def create
@contact = Contact.new(contact_params)
@contact.save
redirect_to @contact
end
private
def contact_params
params.require(:contact).permit(:name, :email, :mob, :head, :content)
end
end
控制台错误
Started GET "/contacts/" for ::1 at 2019-12-16 19:14:52 +0530
Processing by ContactsController#index as HTML
Rendering contacts/index.html.erb within layouts/application
Rendered contacts/index.html.erb within layouts/application (Duration: 1.2ms | Allocations: 1465)
Completed 500 Internal Server Error in 9ms (ActiveRecord: 0.0ms | Allocations: 2685)
ActionView::Template::Error (undefined method `name' for nil:NilClass):
10: <th>contents</th>
11: </tr>
12:
13: <%= @contact.name %>
14: <!--
15: <% @contacts.each do |contact| %>
16: <tr>
联系人模型表
class CreateContacts < ActiveRecord::Migration[6.0]
def change
create_table :contacts do |t|
t.string :name
t.string :email
t.string :mob
t.string :head
t.string :content
t.timestamps
end
end
end
描述联系表mysql> desc联系人;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| email | varchar(255) | YES | | NULL | |
| mob | varchar(255) | YES | | NULL | |
| head | varchar(255) | YES | | NULL | |
| content | varchar(255) | YES | | NULL | |
| created_at | datetime(6) | NO | | NULL | |
| updated_at | datetime(6) | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
**接触模型**
class Contact < ApplicationRecord
end
无论发生什么错误,堆栈溢出都已提供了答案,但是仍然出现此错误。...我从5天起停留在这里,任何人都可以帮我,告诉我我哪里错了。我正在关注Ruby在Rails guid上,但我找不到错误。
[在某些帖子中,我看到他们在索引定义中犯了错误,但是我已经检查出它是正确的。我是这个领域的新手,可能是我正在做一些mmistake,但是无法解决... ...>
index.html.erb
名称 | 电子邮件 | 手机号码 | ...
---|
您在第13行上有一个变量,它导致index.hmtl.erb中的错误
删除以下行,您的代码将起作用:
13: <%= @contact.name %>
快乐编码!
Rails中的索引视图用于显示整个记录集合。
def index
@contacts = Contact.all
end
注意,实例变量被复数称为@contacts
-它的ActiveRecord::Relation
包含所有联系人。但在视图中,您正在调用@contact
:
13: <%= @contact.name %>
14: <!--
15: <% @contacts.each do |contact| %>
删除第13行<%= @contact.name %>
。这里还有很多其他问题。您不会在新方法中创建新实例:
# GET /contacts/new
def new
@contact = Contact.new
end
并且在create方法中,您甚至不必费心检查记录是否确实有效并保存到数据库中:
# POST /contacts
def create
@contact = Contact.new(contact_params)
if @contact.save
redirect_to @contact
else
render :new
end
end
并且您应该将表单绑定到模型实例:
<%= form_with model: @contact, local: true do |form| %>
作为一个友好的建议,如果您在此问题上停留了五天,则需要重新考虑您的学习策略。这实际上是大多数Rails教程所涵盖的基本内容。这些指南是不错的参考,但是您需要逐步说明如何将它们绑在一起。
Rails还提供了一个脚手架生成器,您可以使用它来了解基本CRUD控制器的外观。
例如:
rails g scaffold person name age