NoMethodError - “string”的未定义方法`type':String:

时间:2015-08-05 08:10:15

标签: ruby ruby-on-rails-4 rubygems rails-activerecord simple-form

Daniel Kehoe读过“学习Ruby on Rails”的人吗?如果没有,那么请跳过下一段并直接看到堆栈。

我刚刚使用模型方法在“新建”和“创建”路径的“联系”页面上构建了“Rails Way”应用程序。作者刚刚介绍了使用“Rails Way”验证的有多酷。但是,当我单击菜单中的“联系”链接时,它会抛出一个我目前无法理解的错误。我在下面提供错误的详细信息。您可以提供的任何见解都会非常有用。

任何人遇到以下处理ActiveRecord的错误?

错误:

NoMethodError - undefined method `type' for "string":String:

STACK:

Started GET "/contacts/new" for ::1 at 2015-08-05 00:22:18 -0700
Processing by ContactsController#new as HTML
  Rendered contacts/new.html.erb within layouts/application (1.7ms)
Completed 500 Internal Server Error in 5ms (ActiveRecord: 0.0ms)

NoMethodError - undefined method `type' for "string":String:
  activerecord (4.2.2) lib/active_record/connection_adapters/column.rb:18:in `type'
  activesupport (4.2.2) lib/active_support/core_ext/object/try.rb:77:in `public_send'
  activesupport (4.2.2) lib/active_support/core_ext/object/try.rb:77:in `try!'
  activesupport (4.2.2) lib/active_support/core_ext/object/try.rb:63:in `try'
  simple_form (3.1.0) lib/simple_form/form_builder.rb:514:in `default_input_type'
  simple_form (3.1.0) lib/simple_form/form_builder.rb:497:in `find_input'
  simple_form (3.1.0) lib/simple_form/form_builder.rb:112:in `input'
  app/views/contacts/new.html.erb:6:in `block in _app_views_contacts_new_html_erb___1021484279056782038_70328966650640'
  actionview (4.2.2) lib/action_view/helpers/capture_helper.rb:38:in `block in capture'
  actionview (4.2.2) lib/action_view/helpers/capture_helper.rb:200:in `with_output_buffer'
  actionview (4.2.2) lib/action_view/helpers/capture_helper.rb:38:in `capture'
  actionview (4.2.2) lib/action_view/helpers/form_helper.rb:444:in `form_for'
...

new.html.erb的内容

<%content_for:title do%>联系<%end%>

联系

<%= simple_form_for @contact do | form | %GT; <%= form.error_notification%> <%= form.input:name,autofocus:true%> <%= form.input:email%> <%= form.input:content,as :: text%> <%= form.button:submit,'Submit',class:'submit'%> <%end%>

1 个答案:

答案 0 :(得分:1)

在contact.rb文件中更改此

#column :name, :string
#column :email, :string
#column :content, :string

用4.2.2

中的以下行替换3行以上
attr_accessor :name, :string
attr_accessor :email, :string
attr_accessor :content, :string
相关问题