为什么我收到错误“未定义的方法‘名称’”对于”在 Formtastic / haml 视图中,其中“name”是是模型上的属性吗?

发布于 2024-10-26 06:15:27 字数 1926 浏览 3 评论 0原文

这可能是愚蠢的事情,但我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生生漫 2024-11-02 06:15:27

您有一个名为“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.

戏蝶舞 2024-11-02 06:15:27

尝试再次加载您的架构

rake db:schema:load

Try to load your schema again

rake db:schema:load
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文