ruby on rails - Undefined method 'nested attribute' for model -


i'm trying to create 2 type of addresses: pick address , delivery address. created polymorphic address model associated listing , order. want create new address in listings , orders views using fields for. reason works when create listing, when try create order undefined method 'address' model order.

note: irrelevant code has been omitted snippets below.

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 

in form views have

app/views/listings/_form.html.erb

<%= form_for(setup_listing(@listing)) |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 |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%> 

app/views/orders/_form.html.erb

<%= form_for([@listing, setup_order(@order)]) |f| %>   <%= f.fields_for :delivery_address |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%> 

note in order form_for function use square brackets because build order listing.

routes.rb

rails.application.routes.draw   resources :listings      resources :orders   end end 

the setup_order/listing function comes from

app/helpers/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 

which use create new address in case 1 not exist suggested in this tutorial.

i made sure add address attributes params in controllers

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 

again, creating listing works perfectly. problem happens when try create new order. in advanced!

my error

full trace

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' 

after not finding answers problem diceded change relationship has_one has_many, setting default attribute name, see if error had changed.

class order < applicationrecord    belongs_to :listing   has_many :addresses, as: :addressable, dependent: :destroy    accepts_nested_attributes_for :delivery_address  end 

i made changes in controller , view, again getting no method 'address' instead of 'addresses' should have been. realize somewhere must have written address searched @eeeeeean suggested , indeed in validations (which omitted in questions considering them irrelevant) still had validation address, here actual code model.

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 

so avoid omitting code in questions , make sure check places wouldn't so.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -