enmanuel martinez enmanuel martinez - 25 days ago 11
Ruby Question

Undefined method 'nested attribute' for model

I'm trying to to create 2 type of addresses: pick up address and delivery address. To do this i created a polymorphic address model which is associated to a listing and an order. I want to create the new address in the listings and orders views using fields for. For some reason this is only works when i create a listing, when i try to create an order i get undefined method 'address' for model order.

Note: Irrelevant code has been omitted from the 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 my form views i have

app/views/listings/_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%>


app/views/orders/_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%>


note that in the order form_for function i use square brackets because i build the order from the listing.

routes.rb

Rails.application.routes.draw do
resources :listings do
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 i use to create the new address in case this one does not already exist as suggested in this tutorial.

I made sure to add the address attributes to my params in the 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 a listing works perfectly. The problem happens when i try to create a new order. Thanks 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'

Answer

After not finding any answers for my problem i diceded to change the relationship from has_one to has_many, setting the default attribute name, to see if that error had changed.

class Order < ApplicationRecord

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

  accepts_nested_attributes_for :delivery_address

end

I also made the changes in my controller and view, but again i was getting no method 'address' instead of 'addresses' as it should have been. then i realize that somewhere i must have written address so i searched as @eeeeeean suggested and indeed in the validations (which i omitted in the questions for considering them irrelevant) i still had a validation for address, here the actual code for the 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 the questions and make sure to check places you wouldn't usually do so.

Comments