我是Ruby on Rails的新手,但遇到此错误:
ActionView::Template::Error (undefined method `comment' for #<Contact:0x000000001057cac8>
我已经上网查看了很多关于has_many
的帖子,但是我不认为这是我的错,因为我没有尝试创建评论线程。
我的contacts_controller.rb:
class ContactsController < ApplicationController
def new
@contact = Contact.new
end
def create
@contact = Contact.new(contact_params)
if @contact.save
name = params[:contact][:name]
email = params[:contact][:email]
body = params[:contact][:body]
ContactMailer.contact_email(name, email, body).deliver
flash[:success] = "Message sent."
redirect_to new_contact_path
else
flash[:danger] = "Message not sent."
redirect_to new_contact_path
end
end
private
def contact_params
params.require(:contact).permit(:name, :email, :comment)
end
end
和我的联系人表格:
<div class="row">
<%= form_for @contact do |f| %>
<div class="col-md-4 col-md-offset-4">
<div class="well">
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :comment %>
<%= f.text_area :comment, class: 'form-control' %>
</div>
<%= f.submit 'Submit', class: "btn btn-success btn-lg" %>
</div>
</div>
<% end %>
</div>
我的模型,contact.rb:
class Contact < ActiveRecord::Base
validates :name, presence: true
validates :email, presence: true
validates :comment, presence: true
end
我的数据库资料:
class CreateContacts < ActiveRecord::Migration[6.0]
def change
create_table :contacts do |t|
t.string :name
t.string :email
t.text :comment
t.timestamps
end
end
end
我使用Heroku CLI管理mt应用程序,这是我运行rake db:migrate
DEPRECATION WARNING: Including LoggerSilence is deprecated and will be removed in Rails 6.1. Please use `ActiveSupport::LoggerSilence` instead (called from <top (required)> at /app/config/application.rb:7)
(1.1ms) SELECT pg_try_advisory_lock(1634294230270099020)
(1.5ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
ActiveRecord::InternalMetadata Load (1.6ms) SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 LIMIT $2 [["key", "environment"], ["LIMIT", 1]]
(1.2ms) SELECT pg_advisory_unlock(1634294230270099020)
此外,这在我的本地主机上运行良好,但在Heroku上却没有。是Heroku给我错误,我不知道为什么。
我尝试过的方法(请记住,我是RoR的新手):
确保在本地主机上运行的相同版本是我推送到Heroku的版本。
确保这不是我的路线中的问题。
重置数据库。
确保拼写一致。
我从Heroku日志中得到的错误:
2019-09-12T11:05:54.009358+00:00 app[web.1]: [a382e524-adf1-462b-bff6-bd6cba881a7c] Completed 500 Internal Server Error in 226ms (ActiveRecord: 32.0ms | Allocations: 15274)
2019-09-12T11:05:54.011555+00:00 app[web.1]: [a382e524-adf1-462b-bff6-bd6cba881a7c]
2019-09-12T11:05:54.011558+00:00 app[web.1]: [a382e524-adf1-462b-bff6-bd6cba881a7c] ActionView::Template::Error (undefined method `comment' for #<Contact:0x000055682aab6110>
2019-09-12T11:05:54.011561+00:00 app[web.1]: Did you mean? comments
2019-09-12T11:05:54.011563+00:00 app[web.1]: comments=
2019-09-12T11:05:54.011565+00:00 app[web.1]: comments?):
2019-09-12T11:05:54.011567+00:00 app[web.1]: [a382e524-adf1-462b-bff6-bd6cba881a7c] 14:
2019-09-12T11:05:54.011570+00:00 app[web.1]: [a382e524-adf1-462b-bff6-bd6cba881a7c] 15: <div class="form-group">
2019-09-12T11:05:54.011572+00:00 app[web.1]: [a382e524-adf1-462b-bff6-bd6cba881a7c] 16: <%= f.label :comment %>
2019-09-12T11:05:54.011575+00:00 app[web.1]: [a382e524-adf1-462b-bff6-bd6cba881a7c] 17: <%= f.text_area :comment, class: 'form-control' %>
2019-09-12T11:05:54.011577+00:00 app[web.1]: [a382e524-adf1-462b-bff6-bd6cba881a7c] 18: </div>
2019-09-12T11:05:54.011579+00:00 app[web.1]: [a382e524-adf1-462b-bff6-bd6cba881a7c] 19:
2019-09-12T11:05:54.011581+00:00 app[web.1]: [a382e524-adf1-462b-bff6-bd6cba881a7c] 20: <%= f.submit 'Submit', class: "btn btn-dark btn-block" %>
2019-09-12T11:05:54.011583+00:00 app[web.1]: [a382e524-adf1-462b-bff6-bd6cba881a7c]
2019-09-12T11:05:54.011586+00:00 app[web.1]: [a382e524-adf1-462b-bff6-bd6cba881a7c] app/views/contacts/new.html.erb:17
2019-09-12T11:05:54.011588+00:00 app[web.1]: [a382e524-adf1-462b-bff6-bd6cba881a7c] app/views/contacts/new.html.erb:2
答案 0 :(得分:1)
此代码中没有has_many
关联。只有文本字段可供评论。如果您使用comment
而不是comments
,则不会收到错误消息。
在控制器中:
def contact_params
params.require(:contact).permit(:name, :email, :comment)
end
并查看:
<div class="form-group">
<%= f.label :comment %>
<%= f.text_area :comment, class: 'form-control' %>
</div>
如果要创建has_many
关联,请查看rails指南:has_many association
答案 1 :(得分:0)
我认为您的属性(列)名称为“评论”,而您正在尝试传递“评论”。
应该是
更改控制器
def contact_params
params.require(:contact).permit(:name, :email, :comment)
end
视图变化
<div class="form-group">
<%= f.label :comments %>
<%= f.text_area :comment, class: 'form-control' %>
</div>
答案 2 :(得分:0)
您遇到列重命名问题。不建议重置数据库,因为:
heroku
)您应该做的是创建另一个迁移文件以重命名该列,因此,Rails
在提取更改时将检测到新文件并要求您运行rake db:migrate
并已解决问题。
def up
# rename_column :contacts, :old_column, :new_column
rename_column :contacts, :comments, :comment
end