未定义的方法'嵌套属性'对于模型

时间:2016-11-05 19:43:25

标签: ruby-on-rails ruby polymorphism ruby-on-rails-5

我试图创建两种类型的地址:获取地址和送货地址。为此,我创建了一个与列表和订单相关联的多态地址模型。我想在列表和订单视图中使用字段创建新地址。出于某种原因,这仅在我创建列表时有效,当我尝试创建订单时,我会得到未定义的方法'地址'对于模型订单。

注意:以下摘录中省略了不相关的代码。

address.rb

class Address < ApplicationRecord
 belongs_to :addressable, polymorphic: true, optional: true
end

listing.rb

class Listing < ApplicationRecord
 has_many :orders
 has_one :pickup_address, as: :addressable, class_name: "Address", dependent: :destroy

 accepts_nested_attributes_for :pickup_address
end

order.rb

class Order < ApplicationRecord
 belongs_to :listing
 has_one :delivery_address, as: :addressable, class_name: "Address", dependent: :destroy

 accepts_nested_attributes_for :delivery_address
end

在我的表单视图中我有

应用/视图/列表/ _form.html.erb

<%= form_for(setup_listing(@listing)) do |f| %>
 <%= f.label :description %>
 <%= f.text_area :description, class: 'form-control' %>

 <%= f.label :price %>
 <%= f.text_field :price, class: 'form-control' %>

 <%= f.fields_for :pickup_address do |u| %>

    <%= u.label :city %>
    <%= u.text_field :city, class: 'form-control' %>

    <%= u.label :sector %>
    <%= u.text_field :sector, class: 'form-control' %>

    <%= u.label :line_1 %>
    <%= u.text_field :line_1, class: 'form-control' %>

    <%= u.label :line_2 %>
    <%= u.text_field :line_2, class: 'form-control' %>

   <% end %>
 <%= f.submit "submit", class: "btn btn-primary" %>
<%end%>

应用/视图/命令/ _form.html.erb

<%= form_for([@listing, setup_order(@order)]) do |f| %>
  <%= f.fields_for :delivery_address do |u| %>

  <%= u.label :city %>
  <%= u.text_field :city, class: 'form-control' %>

  <%= u.label :sector %>
  <%= u.text_field :sector, class: 'form-control' %>

  <%= u.label :line_1 %>
  <%= u.text_field :line_1, class: 'form-control' %>

  <%= u.label :line_2 %>
  <%= u.text_field :line_2, class: 'form-control' %>

  <% end %>
 <%= f.submit "submit", class: "btn btn-primary" %>
<%end%>

请注意,在form_for函数中,我使用方括号,因为我从列表中构建了订单。

的routes.rb

Rails.application.routes.draw do
  resources :listings do
     resources :orders
  end
end

setup_order / listing函数来自

应用/助手/ form_helper.rb

 module FormHelper

    def setup_listing(listing)
      listing.build_pickup_address if listing.pickup_address.nil?
      listing
    end

    def setup_order(order)
       order.build_delivery_address if order.delivery_address.nil?
       order
    end

 end

我用来创建新地址,以防这个地址不存在this tutorial中的建议。

我确保将地址属性添加到控制器中的params

listings_controller.rb

  def create
    @listing = current_user.listings.build(listing_params)
    if @listing.save
      flash[:success] = "listing created!"
      redirect_to @listing
    else
      render 'new'
    end
  end
   .
   .

  private

  def listing_params
   params.require(:listing).permit(pickup_address_attributes: [:city,   :sector,:line_1, :line_2])
  end

orders_controller.rb

   .
   .
   def create
     @listing = Listing.find(params[:listing_id])
     @order = @listing.orders.build(order_params)
     if @order.save
       redirect_to listings_path
     else
      render 'new'
     end
   end
   .
   .
   private

   def order_params
     params.require(:order).permit(delivery_address_attributes: [:city, :sector, :line_1, :line_2])
   end

再次,创建列表完美无缺。当我尝试创建新订单时会发生问题。先谢谢!

My error

FUll tr​​ace

activemodel (5.0.0) lib/active_model/attribute_methods.rb:433:in `method_missing'
activemodel (5.0.0) lib/active_model/validator.rb:149:in `block in validate'
activemodel (5.0.0) lib/active_model/validator.rb:148:in `each'
activemodel (5.0.0) lib/active_model/validator.rb:148:in `validate'
activesupport (5.0.0) lib/active_support/callbacks.rb:405:in `public_send'
activesupport (5.0.0) lib/active_support/callbacks.rb:405:in `block in make_lambda'
activesupport (5.0.0) lib/active_support/callbacks.rb:169:in `block (2 levels) in halting'
activesupport (5.0.0) lib/active_support/callbacks.rb:547:in `block (2 levels) in default_terminator'
activesupport (5.0.0) lib/active_support/callbacks.rb:546:in `catch'
activesupport (5.0.0) lib/active_support/callbacks.rb:546:in `block in default_terminator'
activesupport (5.0.0) lib/active_support/callbacks.rb:170:in `block in halting'
activesupport (5.0.0) lib/active_support/callbacks.rb:454:in `block in call'
activesupport (5.0.0) lib/active_support/callbacks.rb:454:in `each'
activesupport (5.0.0) lib/active_support/callbacks.rb:454:in `call'
activesupport (5.0.0) lib/active_support/callbacks.rb:101:in `__run_callbacks__'
activesupport (5.0.0) lib/active_support/callbacks.rb:750:in `_run_validate_callbacks'
activemodel (5.0.0) lib/active_model/validations.rb:408:in `run_validations!'
activemodel (5.0.0) lib/active_model/validations/callbacks.rb:113:in `block in run_validations!'
activesupport (5.0.0) lib/active_support/callbacks.rb:126:in `call'
activesupport (5.0.0) lib/active_support/callbacks.rb:506:in `block (2 levels) in compile'
activesupport (5.0.0) lib/active_support/callbacks.rb:455:in `call'
activesupport (5.0.0) lib/active_support/callbacks.rb:101:in `__run_callbacks__'
activesupport (5.0.0) lib/active_support/callbacks.rb:750:in `_run_validation_callbacks'
activemodel (5.0.0) lib/active_model/validations/callbacks.rb:113:in `run_validations!'
activemodel (5.0.0) lib/active_model/validations.rb:338:in `valid?'
activerecord (5.0.0) lib/active_record/validations.rb:65:in `valid?'
activerecord (5.0.0) lib/active_record/validations.rb:82:in `perform_validations'
activerecord (5.0.0) lib/active_record/validations.rb:44:in `save'
activerecord (5.0.0) lib/active_record/attribute_methods/dirty.rb:22:in `save'
activerecord (5.0.0) lib/active_record/transactions.rb:319:in `block (2 levels) in save'
activerecord (5.0.0) lib/active_record/transactions.rb:395:in `block in with_transaction_returning_status'
activerecord (5.0.0) lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `block in transaction'
activerecord (5.0.0) lib/active_record/connection_adapters/abstract/transaction.rb:189:in `within_new_transaction'
activerecord (5.0.0) lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `transaction'
activerecord (5.0.0) lib/active_record/transactions.rb:211:in `transaction'
activerecord (5.0.0) lib/active_record/transactions.rb:392:in `with_transaction_returning_status'
activerecord (5.0.0) lib/active_record/transactions.rb:319:in `block in save'
activerecord (5.0.0) lib/active_record/transactions.rb:334:in `rollback_active_record_state!'
activerecord (5.0.0) lib/active_record/transactions.rb:318:in `save'
activerecord (5.0.0) lib/active_record/suppressor.rb:41:in `save'
app/controllers/orders_controller.rb:45:in `create'
actionpack (5.0.0) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.0.0) lib/abstract_controller/base.rb:188:in `process_action'
actionpack (5.0.0) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.0.0) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.0.0) lib/active_support/callbacks.rb:126:in `call'
activesupport (5.0.0) lib/active_support/callbacks.rb:506:in `block (2 levels) in compile'
activesupport (5.0.0) lib/active_support/callbacks.rb:455:in `call'
activesupport (5.0.0) lib/active_support/callbacks.rb:101:in `__run_callbacks__'
activesupport (5.0.0) lib/active_support/callbacks.rb:750:in `_run_process_action_callbacks'
activesupport (5.0.0) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.0) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.0.0) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.0.0) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.0.0) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (5.0.0) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.0.0) lib/active_support/notifications.rb:164:in `instrument'
actionpack (5.0.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.0.0) lib/action_controller/metal/params_wrapper.rb:248:in `process_action'
searchkick (1.3.5) lib/searchkick/logging.rb:153:in `process_action'
activerecord (5.0.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (5.0.0) lib/abstract_controller/base.rb:126:in `process'
actionview (5.0.0) lib/action_view/rendering.rb:30:in `process'
actionpack (5.0.0) lib/action_controller/metal.rb:190:in `dispatch'
actionpack (5.0.0) lib/action_controller/metal.rb:262:in `dispatch'
actionpack (5.0.0) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
actionpack (5.0.0) lib/action_dispatch/routing/route_set.rb:32:in `serve'
actionpack (5.0.0) lib/action_dispatch/journey/router.rb:39:in `block in serve'
actionpack (5.0.0) lib/action_dispatch/journey/router.rb:26:in `each'
actionpack (5.0.0) lib/action_dispatch/journey/router.rb:26:in `serve'
actionpack (5.0.0) lib/action_dispatch/routing/route_set.rb:725:in `call'
rack (2.0.1) lib/rack/etag.rb:25:in `call'
rack (2.0.1) lib/rack/conditional_get.rb:38:in `call'
rack (2.0.1) lib/rack/head.rb:12:in `call'
rack (2.0.1) lib/rack/session/abstract/id.rb:222:in `context'
rack (2.0.1) lib/rack/session/abstract/id.rb:216:in `call'
actionpack (5.0.0) lib/action_dispatch/middleware/cookies.rb:613:in `call'
activerecord (5.0.0) lib/active_record/migration.rb:552:in `call'
actionpack (5.0.0) lib/action_dispatch/middleware/callbacks.rb:38:in `block in call'
activesupport (5.0.0) lib/active_support/callbacks.rb:97:in `__run_callbacks__'
activesupport (5.0.0) lib/active_support/callbacks.rb:750:in `_run_call_callbacks'
activesupport (5.0.0) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.0) lib/action_dispatch/middleware/callbacks.rb:36:in `call'
actionpack (5.0.0) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.0) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:49:in `call'
web-console (3.1.1) lib/web_console/middleware.rb:131:in `call_app'
web-console (3.1.1) lib/web_console/middleware.rb:20:in `block in call'
web-console (3.1.1) lib/web_console/middleware.rb:18:in `catch'
web-console (3.1.1) lib/web_console/middleware.rb:18:in `call'
actionpack (5.0.0) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.0.0) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.0.0) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.0.0) lib/active_support/tagged_logging.rb:70:in `block in tagged'
activesupport (5.0.0) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.0.0) lib/active_support/tagged_logging.rb:70:in `tagged'
railties (5.0.0) lib/rails/rack/logger.rb:24:in `call'
sprockets-rails (3.1.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.0.0) lib/action_dispatch/middleware/request_id.rb:24:in `call'
rack (2.0.1) lib/rack/method_override.rb:22:in `call'
rack (2.0.1) lib/rack/runtime.rb:22:in `call'
activesupport (5.0.0) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
actionpack (5.0.0) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.0) lib/action_dispatch/middleware/static.rb:136:in `call'
rack (2.0.1) lib/rack/sendfile.rb:111:in `call'
railties (5.0.0) lib/rails/engine.rb:522:in `call'
puma (3.4.0) lib/puma/configuration.rb:224:in `call'
puma (3.4.0) lib/puma/server.rb:569:in `handle_request'
puma (3.4.0) lib/puma/server.rb:406:in `process_client'
puma (3.4.0) lib/puma/server.rb:271:in `block in run'
puma (3.4.0) lib/puma/thread_pool.rb:114:in `block in spawn_thread'

1 个答案:

答案 0 :(得分:0)

在找不到我的问题的任何答案后,我很想改变从has_one到has_many的关系,设置默认属性名称,以查看该错误是否已更改。

class Order < ApplicationRecord

  belongs_to :listing
  has_many :addresses, as: :addressable, dependent: :destroy

  accepts_nested_attributes_for :delivery_address

end

我也在我的控制器和视图中进行了更改,但我再次没有找到方法&#39;地址&#39;而不是地址&#39;本来应该的。然后我意识到在某个地方我必须有写地址所以我搜索了@eeeeeean建议并且确实在验证中(我在问题中省略了考虑它们无关紧要)我仍然有一个地址验证,这里是模型的实际代码。

class Order < ApplicationRecord
  validates :address, :seller_id, :buyer_id, :listing_id, presence: true  

  belongs_to :listing
  belongs_to :buyer, class_name: "User"
  belongs_to :seller, class_name: "User"

  has_one :delivery_address, as: :addressable, class_name: "Address", dependent: :destroy

  accepts_nested_attributes_for :delivery_address

  enum status: [ :ordered, :shipped, :delivered, :rated ]

end

因此,请避免在问题中省略代码,并确保检查通常不会执行此操作的地方。