为什么我收到错误“未定义的方法‘名称’”对于”在 Formtastic / haml 视图中,其中“name”是是模型上的属性吗?
这可能是愚蠢的事情,但我对 Rails 和 Rails 的了解还不够。红宝石看到了。我有以下架构和;查看但我收到下面提到的错误。业务继承自设计帐户,因此这就是电子邮件和信息的所在。密码来自。
任何帮助将不胜感激,谢谢!
架构:
create_table "businesses", :force => true do |t|
t.string "name"
t.string "street"
t.string "city"
t.string "zip"
t.datetime "created_at"
t.datetime "updated_at"
end
视图:
#registrationForm
-semantic_form_for(resource, :as => resource_name, :url=> registration_path(resource_name)) do |f|
=f.input :name
=f.input :email
=f.input :password
=f.input :password_confirmation
=f.buttons
错误:
undefined method 'name' for
<Business:0x000000052690f8 > Extracted source (around line #3):
编辑
控制器
class BusinessesController < Devise::RegistrationsController
respond_to :html
def new
super
@business = Business.new
end
end
Routes.rb
devise_for :accounts
devise_for :businesses, :controllers => { :registrations => "businesses" }
模型
class Business < Account
end
重新加载架构后的控制台
k = Business.new ( :name =>"test" )
^
(irb):1: syntax error, unexpected ')', expecting $end
from /home/chance/.rvm/gems/ruby-1.9.2-p180@global/gems/railties-3.0.5/lib/rails/commands/console.rb:44:in `start'
from /home/chance/.rvm/gems/ruby-1.9.2-p180@global/gems/railties-3.0.5/lib/rails/commands/console.rb:8:in `start'
from /home/chance/.rvm/gems/ruby-1.9.2-p180@global/gems/railties-3.0.5/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
This is probably something stupid, but I don't know nearly enough about rails & ruby to see it. I have the following schema & view but I am getting the error mentioned below. Business inherits from a Devise Account so thats where the email & password come from.
Any help would be greatly appreciated, thanks!
schema:
create_table "businesses", :force => true do |t|
t.string "name"
t.string "street"
t.string "city"
t.string "zip"
t.datetime "created_at"
t.datetime "updated_at"
end
View:
#registrationForm
-semantic_form_for(resource, :as => resource_name, :url=> registration_path(resource_name)) do |f|
=f.input :name
=f.input :email
=f.input :password
=f.input :password_confirmation
=f.buttons
Error:
undefined method 'name' for
<Business:0x000000052690f8 > Extracted source (around line #3):
Edit
Controller
class BusinessesController < Devise::RegistrationsController
respond_to :html
def new
super
@business = Business.new
end
end
Routes.rb
devise_for :accounts
devise_for :businesses, :controllers => { :registrations => "businesses" }
Model
class Business < Account
end
console after reloading schema
k = Business.new ( :name =>"test" )
^
(irb):1: syntax error, unexpected ')', expecting $end
from /home/chance/.rvm/gems/ruby-1.9.2-p180@global/gems/railties-3.0.5/lib/rails/commands/console.rb:44:in `start'
from /home/chance/.rvm/gems/ruby-1.9.2-p180@global/gems/railties-3.0.5/lib/rails/commands/console.rb:8:in `start'
from /home/chance/.rvm/gems/ruby-1.9.2-p180@global/gems/railties-3.0.5/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有一个名为“accounts”的表和一个名为“businesses”的表。
帐户是由设备创建的,具有其所有属性,并指向“帐户”表。
Business 继承自 Account,因此使用 Rails 的 STI(单表继承)特征。因此它也指向“帐户”表。
如果您要
商务
ActiveRecord::Base
它将指向您的“商家”表。 ActiveRecord的STI机制很奇怪。我认为您需要更多地考虑您希望数据模型如何工作。也许企业应该
属于:account
并有一个相应的:account_id。或者您可以将所有“企业”列添加到帐户表中。
You have a table named 'accounts' and a table named 'businesses'.
Account is being made by devise, and has all its attributes, and points to the 'accounts' table.
Business inherits from Account, and therefore is using Rails' STI (single table inheritance) features. It therefore points to the 'accounts' table as well.
If you were to have
Business < ActiveRecord::Base
it would point to your 'businesses' table. ActiveRecord's STI mechanism is very strange.I think you need to think more about how you want your data model to work. Perhaps Business should
belong_to :account
and have an according :account_id.Either that or you could add all the 'businesses' columns to the accounts table.
尝试再次加载您的架构
Try to load your schema again