NoMethodError Exception: undefined method `order_item' for #<Order:0x00000009dd52a8> - ruby-on-rails

I have three models, Order, order_item, and food_item
An order has_many order_items and an order_item has_many food_items
When I create a new order from my order_controller API I am getting the below error which am not sure whats wrong.
NoMethodError Exception: undefined method `order_item' for #<Order:0x00000009dd52a8>
orders_controller.rb
class Api::V1::OrdersController < Api::V1::ApplicationController
protect_from_forgery with: :null_session
skip_before_filter :verify_authenticity_token, :if => Proc.new {|c| c.request.format.json? }
before_action :fetch_restaurent
before_action :fetch_food_items, :only => [:index]
after_action :send_order_to_kitchen, :only => [:create, :update]
def index
#orders = Restaurant.orders.fetch_items_from_ethor
end
def show
# Display the list of food_items available from the restaurant
# Allow the customer to place an order
end
def create
#order = Order.new(order_params)
if #order.save
render :json, order, status:201, location: [:api, current_customer, order]
else
render :json, { errors: order.errors }, status:422
end
end
# once the order is placed just send it to the kitechn()
def update
#order = Order.find_by(params[:id])
#rder.update_attributes(order_params)
if #order.save
render :json, order, status: 201, location: [:api, current_user.order.id]
else
render :json,{errors: order.errors}, status: 422
end
end
private
def order_params
params.require(:order).permit(:customer_id, :order_id, :pos_id, :table_id, :order_number,
:order_status,:order_date, :food_item_ids)
end
end
order.rb # model file
class Order < ActiveRecord::Base
belongs_to :customer
belongs_to :restaurant
belongs_to :table
has_many :order_items
has_many :food_items, through: :order_items
validates :customer_id, :order_number, presence:true
validates :subtotal, :final_total, presence:true, numericality:{ greater_than_or_equal_to:0 }
end
order_item.rb # model
class OrderItem < ActiveRecord::Base
belongs_to :food_item, inverse_of: :order_items
belongs_to :order, inverse_of: :order_items
belongs_to :restaurant
has_many :food_items
end
food_item.rb # model
class FoodItem < ActiveRecord::Base
belongs_to :restaurant
belongs_to :order, inverse_of: :food_items
belongs_to :order_item, inverse_of: :food_items
has_many :categories_food_items
has_many :categories, through: :categories_food_items
end
I do not understand from where it is asking for order_item. Please help me to understand fix this problem.
undefined method `order_item' for #<Order:0x00000009dd52a8>
My stack trace:
Started POST "/api/v1/orders?access_token=testing&order[customer_id]=1&order[order_number]=101&order[pos_id]=222&order[table_id]=1&order[order_status]=pending&order[order_date]=02/03/2015" for 127.0.0.1 at 2015-06-22 14:39:30 +0530
ActiveRecord::SchemaMigration Load (0.1ms) SELECT `schema_migrations`.* FROM `schema_migrations`
Processing by Api::V1::OrdersController#create as JSON
Parameters: {"access_token"=>"testing", "order"=>{"customer_id"=>"1", "order_number"=>"101", "pos_id"=>"222", "table_id"=>"1", "order_status"=>"pending", "order_date"=>"02/03/2015"}}
ApiKey Load (0.1ms) SELECT `api_keys`.* FROM `api_keys` WHERE `api_keys`.`access_token` = 'testing' LIMIT 1
Restaurant Load (0.1ms) SELECT `restaurants`.* FROM `restaurants` WHERE `restaurants`.`id` IS NULL LIMIT 1
Completed 500 Internal Server Error in 35ms
** [Airbrake] Notice was not sent due to configuration:
Environment Monitored? false
API key set? true
NoMethodError - undefined method `order_items' for #<Class:0x0000000a70f0f8>:
activerecord (4.1.8) lib/active_record/dynamic_matchers.rb:26:in `method_missing'
app/controllers/api/v1/orders_controller.rb:24:in `create'
actionpack (4.1.8) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.1.8) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.1.8) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.1.8) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:215:in `block in halting_and_conditional'
activesupport (4.1.8) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
activesupport (4.1.8) lib/active_support/callbacks.rb:86:in `run_callbacks'
actionpack (4.1.8) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
searchkick (0.8.7) lib/searchkick/logging.rb:107:in `process_action'
activerecord (4.1.8) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.1.8) lib/abstract_controller/base.rb:136:in `process'
actionview (4.1.8) lib/action_view/rendering.rb:30:in `process'
actionpack (4.1.8) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.1.8) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.1.8) lib/action_controller/metal.rb:232:in `block in action'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:50:in `call'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:73:in `block in call'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:678:in `call'
xray-rails (0.1.16) lib/xray/middleware.rb:37:in `call'
omniauth (1.2.2) lib/omniauth/strategy.rb:186:in `call!'
omniauth (1.2.2) lib/omniauth/strategy.rb:164:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.5.3) lib/rack/etag.rb:23:in `call'
rack (1.5.3) lib/rack/conditionalget.rb:35:in `call'
rack (1.5.3) lib/rack/head.rb:11:in `call'
remotipart (1.2.1) lib/remotipart/middleware.rb:27:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.3) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.3) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.8) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.8) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.8) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.8) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
airbrake (4.2.0) lib/airbrake/rails/middleware.rb:13:in `call'
better_errors (2.1.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
better_errors (2.1.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
better_errors (2.1.1) lib/better_errors/middleware.rb:57:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.8) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.8) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.8) lib/rails/rack/logger.rb:20:in `call'
quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
actionpack (4.1.8) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.3) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.3) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.8) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.3) lib/rack/lock.rb:17:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/static.rb:84:in `call'
rack (1.5.3) lib/rack/sendfile.rb:112:in `call'
airbrake (4.2.0) lib/airbrake/user_informer.rb:16:in `_call'
airbrake (4.2.0) lib/airbrake/user_informer.rb:12:in `call'
railties (4.1.8) lib/rails/engine.rb:514:in `call'
railties (4.1.8) lib/rails/application.rb:144:in `call'
rack (1.5.3) lib/rack/content_length.rb:14:in `call'
thin (1.5.1) lib/thin/connection.rb:81:in `block in pre_process'
thin (1.5.1) lib/thin/connection.rb:79:in `pre_process'
thin (1.5.1) lib/thin/connection.rb:54:in `process'
thin (1.5.1) lib/thin/connection.rb:39:in `receive_data'
eventmachine (1.0.5) lib/eventmachine.rb:187:in `run'
thin (1.5.1) lib/thin/backends/base.rb:63:in `start'
thin (1.5.1) lib/thin/server.rb:159:in `start'
rack (1.5.3) lib/rack/handler/thin.rb:16:in `run'
rack (1.5.3) lib/rack/server.rb:264:in `start'
railties (4.1.8) lib/rails/commands/server.rb:69:in `start'
railties (4.1.8) lib/rails/commands/commands_tasks.rb:81:in `block in server'
railties (4.1.8) lib/rails/commands/commands_tasks.rb:76:in `server'
railties (4.1.8) lib/rails/commands/commands_tasks.rb:40:in `run_command!'
railties (4.1.8) lib/rails/commands.rb:17:in `<top (required)>'
bin/rails:4:in `<main>'

I figured out the reason why it is saying undefined methodorder_item' for #`
I am actually validating the order_item inside the order model. Which am not supposed to.
After changing my order model as below(removing validation on order_item) the problem got solved.
class Order < ActiveRecord::Base
belongs_to :customer
belongs_to :restaurant
belongs_to :table
has_many :order_items
has_many :food_items, through: :order_items
validates :customer_id, :order_number, presence:true
validates :subtotal, :final_total, presence:true, numericality:{ greater_than_or_equal_to:0 }

Related

undefined method `each' for "27":String

I'm trying to get a number of users to be registered in a team , I updated my code so that a team can get registered, however the following error shows up :
undefined method `each' for "27":String
NoMethodError in TeamsController#create
My code is as follows:
class Team<ActiveRecord::Base
belongs_to :league
belongs_to :seed
has_many :speakers do
def user(level="1")
find_by(level: level).user
end
end
end
my user model looks like this :
class User < ActiveRecord::Base
belongs_to :team
end
user model:
class User<ActiveRecord::Base
has_many :speaking_engagements, class_name: "Speaker"
has_many :teams , through: :speaking_engagements
end
speaker model:
class Speaker < ActiveRecord::Base
belongs_to :team
belongs_to :user
end
Team Controller:
class TeamsController<ApplicationController
def new
#seed=Seed.find_by_id(params[:seed_id])
#league=current_admin.league
#team=current_admin.league.teams.build(:seed_id=>#seed,:approved=>false)
#usernames= #mca.connections.connected.each do |x| x.user end
end
def create
#league=current_admin.league
**#team = #league.teams.build(team_params)** #problem appears to be here
if #team.save
flash[:notice] = "Team Request Sent!."
redirect_to '/'
else
flash[:error] = "Unable to request team."
redirect_to :back
end
end
form looks like:
<div class="panel-body">
<div class="container">
<%= form_for #team do |f| %>
<%= f.hidden_field :seed_id, :value => #seed.id %>
<%= f.hidden_field :league_id, :value => #league.id %>
<div class="row">
<!-- <div class="col-md-8"> -->
<div class="form-group">
<%= f.collection_select :speakers, #usernames,:user_id,:fullname, multiple:true %>
</div>
<!-- </div> -->
</div>
<div class="actions">
<%= f.submit "Create" , class:"btn btn-primary" %>
</div>
<% end %>
</div>
</div>
Speaker migration:
class CreateSpeekers < ActiveRecord::Migration
def change
create_table :speakers do |t|
t.integer :team_id
t.integer :user_id
t.integer :level
t.timestamps null: false
end
end
end
I've been stuck with this error for quite sometime now, I would much appreciate any help!
Full trace:
activerecord (4.2.0) lib/active_record/associations/collection_association.rb:355:in `replace'
activerecord (4.2.0) lib/active_record/associations/collection_association.rb:47:in `writer'
activerecord (4.2.0) lib/active_record/associations/builder/association.rb:123:in `speekers='
activerecord (4.2.0) lib/active_record/attribute_assignment.rb:54:in `public_send'
activerecord (4.2.0) lib/active_record/attribute_assignment.rb:54:in `_assign_attribute'
activerecord (4.2.0) lib/active_record/attribute_assignment.rb:41:in `block in assign_attributes'
actionpack (4.2.0) lib/action_controller/metal/strong_parameters.rb:183:in `each_pair'
actionpack (4.2.0) lib/action_controller/metal/strong_parameters.rb:183:in `each_pair'
activerecord (4.2.0) lib/active_record/attribute_assignment.rb:35:in `assign_attributes'
activerecord (4.2.0) lib/active_record/core.rb:557:in `init_attributes'
activerecord (4.2.0) lib/active_record/core.rb:280:in `initialize'
activerecord (4.2.0) lib/active_record/inheritance.rb:61:in `new'
activerecord (4.2.0) lib/active_record/inheritance.rb:61:in `new'
activerecord (4.2.0) lib/active_record/reflection.rb:131:in `build_association'
activerecord (4.2.0) lib/active_record/associations/association.rb:247:in `build_record'
activerecord (4.2.0) lib/active_record/associations/collection_association.rb:136:in `build'
activerecord (4.2.0) lib/active_record/associations/collection_proxy.rb:254:in `build'
app/controllers/teams_controller.rb:13:in `create'
actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.2.0) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.0) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.0) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:92:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:92:in `_run_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_process_action_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.0) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.0) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.0) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.0) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.2.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.0) lib/action_controller/metal.rb:236:in `block in action'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:42:in `serve'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:802:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.6.4) lib/rack/etag.rb:24:in `call'
rack (1.6.4) lib/rack/conditionalget.rb:38:in `call'
rack (1.6.4) lib/rack/head.rb:13:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/flash.rb:260:in `call'
rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.0) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:647:in `call'
activerecord (4.2.0) lib/active_record/migration.rb:378:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `_run_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_call_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
web-console (2.2.1) lib/web_console/middleware.rb:39:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.0) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.0) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.0) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/static.rb:113:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.0) lib/rails/engine.rb:518:in `call'
railties (4.2.0) lib/rails/application.rb:164:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
/home/shyam/.rbenv/versions/2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
/home/shyam/.rbenv/versions/2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
/home/shyam/.rbenv/versions/2.2.1/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'
The problem is that "27" (a user id) is being assigned as the speakers association for a team. This is wrong. speakers should be assigned instances of Speaker.
Unfortunately it gets tough at this point.
What you really need to do is use nested attributes to build the desired speaker records, where each speaker is assign one of the selected user ids. There are many ways to approach this in the UI. There are many great examples of using nested attributes, including this Railscasts episode
has_many gives you a lot of interesting methods, including collection_singular_ids and collection_singular_ids=. The gist of which means you should be able to leverage that getter & setter in your form like this
<%= f.collection_select :speaker_ids, #usernames, :user_id, :fullname, multiple: true %>
i found the answer to my question, thanks to following #WizardOfOgz comment,
so i did the following:
in my teams_controller.rb added the following parameters as permitted :
params.require(:team).permit(:user_id, :league_id,:seed_id, :approved, :speakers_attributes=>[:team_id,:user_id])
end
then,
in my team.rb added the follwing :
accepts_nested_attributes_for :speakers
further, added nested attributes in the form for team:
(in app/views/teams/_form.html.rb )
<h2>Speakers</h2>
<%= f.fields_for :speakers do |ff| %>
<div>
<%= ff.collection_select :user_id, #usernames,:user_id,:fullname%>
</div>
<% end %>
also since I wanted 3 speakers, I created the following function in helpers/form_helpers:
module FormHelper
def setup_team(team)
3.times{
team.speakers.build
}
team
end
end
that solved it for me,
thanks to #WizardOfOgz and #RichPeck
also this one really really helped:
http://www.sitepoint.com/complex-rails-forms-with-nested-attributes/

Calling destroy method gives error -undefined method `name' for nil:NilClass - Rails 4

While trying to delete a product, I am getting this error
NoMethodError in ProductsController#destroy
undefined method `name' for nil:NilClass
Controller:
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
def destroy
#product.destroy <-------error highlights on this line
respond_to do |format|
format.html { redirect_to some_path }
format.json { head :no_content }
end
end
private
def set_product
#product = Product.find(params[:id])
end
Model:
class Product < ActiveRecord::Base
belongs_to :category
belongs_to :sub_category
has_one :item , :dependent => :delete
has_many :carts, :dependent => :delete_all
has_many :assets , :dependent => :delete_all
end
View:
<%= link_to image_tag('delete.png'), product, method: :delete, data: { confirm: 'Are you sure?' } %>
Upon clicking 'OK' from the pop-up, it throws above described error.#product exists,I have checked.
In development mode, it runs fine, but in Digital Ocean under development mode, it is throwing this error. Please help me to figure it out.
Application trace:
app/controllers/products_controller.rb:225:in `destroy'
Full trace:
activerecord (4.0.1) lib/active_record/associations/has_many_association.rb:81:in `cached_counter_attribute_name'
activerecord (4.0.1) lib/active_record/associations/has_many_association.rb:104:in `inverse_updates_counter_cache?'
activerecord (4.0.1) lib/active_record/associations/has_many_association.rb:114:in `delete_records'
activerecord (4.0.1) lib/active_record/associations/collection_association.rb:493:in `remove_records'
activerecord (4.0.1) lib/active_record/associations/collection_association.rb:486:in `block in delete_or_destroy'
activerecord (4.0.1) lib/active_record/associations/collection_association.rb:152:in `block in transaction'
activerecord (4.0.1) lib/active_record/connection_adapters/abstract/database_statements.rb:200:in `transaction'
activerecord (4.0.1) lib/active_record/transactions.rb:209:in `transaction'
activerecord (4.0.1) lib/active_record/associations/collection_association.rb:151:in `transaction'
activerecord (4.0.1) lib/active_record/associations/collection_association.rb:486:in `delete_or_destroy'
activerecord (4.0.1) lib/active_record/associations/collection_association.rb:247:in `destroy'
activerecord (4.0.1) lib/active_record/associations/collection_association.rb:170:in `destroy_all'
activerecord (4.0.1) lib/active_record/associations/has_many_association.rb:26:in `handle_dependency'
activerecord (4.0.1) lib/active_record/associations/builder/association.rb:97:in `has_many_dependent_for_assets'
activesupport (4.0.1) lib/active_support/callbacks.rb:397:in `_run__3353912714138952721__destroy__callbacks'
activesupport (4.0.1) lib/active_support/callbacks.rb:80:in `run_callbacks'
activerecord (4.0.1) lib/active_record/callbacks.rb:292:in `destroy'
activerecord (4.0.1) lib/active_record/transactions.rb:265:in `block in destroy'
activerecord (4.0.1) lib/active_record/transactions.rb:326:in `block in with_transaction_returning_status'
activerecord (4.0.1) lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
activerecord (4.0.1) lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
activerecord (4.0.1) lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
activerecord (4.0.1) lib/active_record/transactions.rb:209:in `transaction'
activerecord (4.0.1) lib/active_record/transactions.rb:323:in `with_transaction_returning_status'
activerecord (4.0.1) lib/active_record/transactions.rb:265:in `destroy'
app/controllers/products_controller.rb:225:in `destroy'
actionpack (4.0.1) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.0.1) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.0.1) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.0.1) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (4.0.1) lib/active_support/callbacks.rb:493:in `_run__3348285909198205465__process_action__callbacks'
activesupport (4.0.1) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.1) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (4.0.1) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.0.1) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.0.1) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.0.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.0.1) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.0.1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.0.1) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
activerecord (4.0.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.0.1) lib/abstract_controller/base.rb:136:in `process'
actionpack (4.0.1) lib/abstract_controller/rendering.rb:44:in `process'
actionpack (4.0.1) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.0.1) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.0.1) lib/action_controller/metal.rb:231:in `block in action'
actionpack (4.0.1) lib/action_dispatch/routing/route_set.rb:80:in `call'
actionpack (4.0.1) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
actionpack (4.0.1) lib/action_dispatch/routing/route_set.rb:48:in `call'
actionpack (4.0.1) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.0.1) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.0.1) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.0.1) lib/action_dispatch/routing/route_set.rb:680:in `call'
rack (1.6.4) lib/rack/deflater.rb:35:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.6.4) lib/rack/etag.rb:24:in `call'
rack (1.6.4) lib/rack/conditionalget.rb:38:in `call'
rack (1.6.4) lib/rack/head.rb:13:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/flash.rb:241:in `call'
rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/cookies.rb:486:in `call'
activerecord (4.0.1) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.0.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
activerecord (4.0.1) lib/active_record/migration.rb:369:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.0.1) lib/active_support/callbacks.rb:373:in `_run__753779113942060240__call__callbacks'
activesupport (4.0.1) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.1) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/reloader.rb:64:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.0.1) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.1) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.0.1) lib/active_support/tagged_logging.rb:67:in `block in tagged'
activesupport (4.0.1) lib/active_support/tagged_logging.rb:25:in `tagged'
activesupport (4.0.1) lib/active_support/tagged_logging.rb:67:in `tagged'
railties (4.0.1) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.0.1) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.0.1) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.0.1) lib/rails/engine.rb:511:in `call'
railties (4.0.1) lib/rails/application.rb:97:in `call'
rack (1.6.4) lib/rack/tempfile_reaper.rb:15:in `call'
rack (1.6.4) lib/rack/lint.rb:49:in `_call'
rack (1.6.4) lib/rack/lint.rb:37:in `call'
rack (1.6.4) lib/rack/showexceptions.rb:24:in `call'
rack (1.6.4) lib/rack/commonlogger.rb:33:in `call'
rack (1.6.4) lib/rack/chunked.rb:54:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
unicorn (5.0.0) lib/unicorn/http_server.rb:562:in `process_client'
unicorn (5.0.0) lib/unicorn/http_server.rb:658:in `worker_loop'
unicorn (5.0.0) lib/unicorn/http_server.rb:508:in `spawn_missing_workers'
unicorn (5.0.0) lib/unicorn/http_server.rb:132:in `start'
unicorn (5.0.0) bin/unicorn:126:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.2.1/bin/unicorn:23:in `load'
/usr/local/rvm/gems/ruby-2.2.1/bin/unicorn:23:in `<main>'
/usr/local/rvm/gems/ruby-2.2.1#global/bin/ruby_executable_hooks:15:in `eval'
/usr/local/rvm/gems/ruby-2.2.1#global/bin/ruby_executable_hooks:15:in `<main>'
item.rb
class Item < ActiveRecord::Base
belongs_to :user
belongs_to :order
belongs_to :product
end
cart.rb
class Cart < ActiveRecord::Base
belongs_to :user
belongs_to :product
end
asset.rb
class Asset < ActiveRecord::Base
mount_uploader :asset1, AssetUploader
mount_uploader :asset2, AssetUploader
mount_uploader :asset3, AssetUploader
mount_uploader :asset4, AssetUploader
end
If I remove :dependent => :delete_all, then this error is gone, but I need to delete the dependents too.
How to get that done?
Hey the thing is that you re using the dependent destroy option whith a has_many association, which is cant be done. Thid option is available only for a belongs_to association.
Like a User has many posts, and a Post belongs to User. You want to set dependent destroy on Post, cause you re saying : ** I want this association to depend on User in destroy method ** , by cons when deleting a user, all his posts should be deleted with.
I hope i could have halped clearing things for you

undefined method `title' for #<Comment:0xbe9aba0>

I just started rails, every things was working fine and suddenly I got below error.
undefined method `title' for #< Comment:0xbe9aba0>
I tried other posts and did not figure out what is wrong.
Extracted source (around line #6):
#article = Article.find(params[:article_id])//line 4
#comment = #article.comments.new(comment_params)//line 5
**#comment.save** //line 6
redirect_to article_path(#article) //line 7
end
stack trace is below:
activemodel (4.1.8) lib/active_model/attribute_methods.rb:435:in `method_missing'
activerecord (4.1.8) lib/active_record/attribute_methods.rb:213:in `method_missing'
activemodel (4.1.8) lib/active_model/validator.rb:165:in `block in validate'
activemodel (4.1.8) lib/active_model/validator.rb:164:in `each'
activemodel (4.1.8) lib/active_model/validator.rb:164:in `validate'
activerecord (4.1.8) lib/active_record/validations/presence.rb:5:in `validate'
activesupport (4.1.8) lib/active_support/callbacks.rb:447:in `public_send'
activesupport (4.1.8) lib/active_support/callbacks.rb:447:in `block in make_lambda'
activesupport (4.1.8) lib/active_support/callbacks.rb:184:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:184:in `block in simple'
activesupport (4.1.8) lib/active_support/callbacks.rb:86:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:86:in `run_callbacks'
activemodel (4.1.8) lib/active_model/validations.rb:376:in `run_validations!'
activemodel (4.1.8) lib/active_model/validations/callbacks.rb:111:in `block in run_validations!'
activesupport (4.1.8) lib/active_support/callbacks.rb:82:in `run_callbacks'
activemodel (4.1.8) lib/active_model/validations/callbacks.rb:111:in `run_validations!'
activemodel (4.1.8) lib/active_model/validations.rb:317:in `valid?'
activerecord (4.1.8) lib/active_record/validations.rb:70:in `valid?'
activerecord (4.1.8) lib/active_record/validations.rb:77:in `perform_validations'
activerecord (4.1.8) lib/active_record/validations.rb:51:in `save'
activerecord (4.1.8) lib/active_record/attribute_methods/dirty.rb:21:in `save'
activerecord (4.1.8) lib/active_record/transactions.rb:268:in `block (2 levels) in save'
activerecord (4.1.8) lib/active_record/transactions.rb:329:in `block in with_transaction_returning_status'
activerecord (4.1.8) lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `block in transaction'
activerecord (4.1.8) lib/active_record/connection_adapters/abstract/database_statements.rb:209:in `within_new_transaction'
activerecord (4.1.8) lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `transaction'
activerecord (4.1.8) lib/active_record/transactions.rb:208:in `transaction'
activerecord (4.1.8) lib/active_record/transactions.rb:326:in `with_transaction_returning_status'
activerecord (4.1.8) lib/active_record/transactions.rb:268:in `block in save'
activerecord (4.1.8) lib/active_record/transactions.rb:283:in `rollback_active_record_state!'
activerecord (4.1.8) lib/active_record/transactions.rb:267:in `save'
app/controllers/comments_controller.rb:6:in `create'
actionpack (4.1.8) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.1.8) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.1.8) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.1.8) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:86:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:86:in `run_callbacks'
actionpack (4.1.8) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.1.8) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.1.8) lib/abstract_controller/base.rb:136:in `process'
actionview (4.1.8) lib/action_view/rendering.rb:30:in `process'
actionpack (4.1.8) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.1.8) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.1.8) lib/action_controller/metal.rb:232:in `block in action'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:82:in `call'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:50:in `call'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:73:in `block in call'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:678:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:35:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.8) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.8) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.8) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.8) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.8) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.8) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.8) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.8) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/static.rb:84:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.8) lib/rails/engine.rb:514:in `call'
railties (4.1.8) lib/rails/application.rb:144:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/home/novin/.rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/home/novin/.rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/home/novin/.rvm/rubies/ruby-2.1.3/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
Comment class:
class CommentsController < ApplicationController
def create
#article = Article.find(params[:article_id])
#comment = #article.comments.new(comment_params)
#comment.save
redirect_to article_path(#article)
end
private
def comment_params
params.require(:comment).permit(:name, :msg, :article_id)
end
end
Article class
class ArticlesController < ApplicationController
def index
#articles = Article.all
end
def show
#article = Article.find(params[:id])
end
end
Let me know if you guys need more details
class Comment < ActiveRecord::Base
belongs_to :article
validates :title, presence: true, length: {minimum: 5}
end
One of the following:
you have a show or index view with a call to name i.e comment.title
you have a validation on name in your comments model
but hard to tell from the info you have given. where do you get the error?
I suspect Comment doesn't have a column called title. Try this to check:
$ rails console
> Comment.column_names
and see if the array includes "title"
The main problem is with your Comment Model. Line number 3 validates :title, presence: true, length: {minimum: 5} Your are not having title column in your comment table.
As you below method, Clearly stated that you don't have title column in your Comment model.
def comment_params
params.require(:comment).permit(:name, :msg, :article_id)
end
So remove validates :title, presence: true, length: {minimum: 5} from your Comment model and all would be good.

undefined scope error in FullCalendar-rails & Postgres for dbm

I'm breaking my head.
I was able to implement fullcalendar-rails using sqlite3. When I installed it in my app (which uses Postgresql) I started having major problems.
When I try to view my calendar at localhost:3000/events/ I get an 'undefined method' error.
Event class:
class Event < ActiveRecord::Base
scope :between, lambda {|start_time, end_time|
{:conditions => ["? < starts_at and starts_at < ?", Event.format_date(start_time), Event.format_date(end_time)] }
}
# need to override the json view to return what full_calendar is expecting.
# http://arshaw.com/fullcalendar/docs/event_data/Event_Object/
def as_json(options = {})
{
:id => self.id,
:title => self.title,
:description => self.description || "",
:start => start_time.rfc822,
:end => end_time.rfc822,
:allDay => self.all_day,
:recurring => false,
:url => Rails.application.routes.url_helpers.event_path(id),
#:color => "red"
}
end
def self.between(start_time, end_time)
where('starts_at > :lo and starts_at < :up',
:lo => Event.format_date(start_time),
:up => Event.format_date(end_time)
)
end
end
What I've tried
this is the part of the event_controller that the error references (the "line 5"):
#events = Event.scoped
If I make it:
#events = Event.all
then the error message goes away, but no calendar renders.
I cannot get the calendar to render AT ALL in my app on Postgresql and I've tried substituting the "starts_at" text in the scope:
class EventsController < ApplicationController
# GET /events
# GET /events.json
def index
#events = Event.scoped
#events = Event.between(params['start'], params['end']) if (params['start'] && params['end'])
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #events }
end
end
Full trace:
activerecord (4.1.1) lib/active_record/dynamic_matchers.rb:26:in `method_missing'
app/controllers/events_controller.rb:5:in `index'
actionpack (4.1.1) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.1.1) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.1.1) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.1.1) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.1.1) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:86:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:86:in `run_callbacks'
actionpack (4.1.1) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.1.1) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.1.1) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.1) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.1.1) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.1.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.1.1) lib/abstract_controller/base.rb:136:in `process'
actionview (4.1.1) lib/action_view/rendering.rb:30:in `process'
actionpack (4.1.1) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.1.1) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.1.1) lib/action_controller/metal.rb:231:in `block in action'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:80:in `call'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:48:in `call'
actionpack (4.1.1) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.1.1) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.1) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:676:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.1) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.1) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.1) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.1) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.1) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.1) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.1) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.1) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.1) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.1) lib/rails/engine.rb:514:in `call'
railties (4.1.1) lib/rails/application.rb:144:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'

undefined method `[]' for nil:NilClass - form

I want to add images as attachment to comments. Comments belong to a Post. I've looked at this gist. I've also tried with nested_form gem, but still the same error.
form
<%= semantic_form_for([#post, #comment], validate: true, html: {multipart: true}, 'data-update-target' => 'comments', class: 'comments') do |f| %>
<%= f.text_field :title %>
<%= f.semantic_fields_for :comment_images do |pic| %>
<%= pic.inputs do %>
<%= pic.hidden_field :id %>
<%= pic.hidden_field :attachable_id %>
<%= pic.hidden_field :attachable_type %>
<%= pic.file_field :image %>
<% end %>
<% end %>
...
comment_image model
class CommentImage < ActiveRecord::Base
belongs_to :attachable, polymorphic: true
attr_accessible :image, :attachable_id, :attachable_type
mount_uploader :image, ImageUploader
end
comment model
class Comment < ActiveRecord::Base
attr_accessor :image, :file
has_many :comment_images, :as => :attachable, :dependent => :destroy
accepts_nested_attributes_for :comment_images, :allow_destroy => true
server log
Started GET "/posts/1/comments/new" for ....136 at 2014-04-28 16:41:47 +0000
Processing by CommentsController#new as HTML
Parameters: {"post_id"=>"1"}
Post Load (0.3ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", "1"]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
params********************{"action"=>"new", "controller"=>"comments", "post_id"=>"1"}
Completed 500 Internal Server Error in 9ms
NoMethodError - undefined method `[]' for nil:NilClass:
activerecord (4.0.4) lib/active_record/attribute_methods/read.rb:86:in `block in read_attribute'
activerecord (4.0.4) lib/active_record/attribute_methods/read.rb:84:in `read_attribute'
activerecord (4.0.4) lib/active_record/attribute_methods.rb:344:in `[]'
activerecord (4.0.4) lib/active_record/associations/association_scope.rb:73:in `block in add_constraints'
activerecord (4.0.4) lib/active_record/associations/association_scope.rb:44:in `add_constraints'
activerecord (4.0.4) lib/active_record/associations/association_scope.rb:19:in `scope'
activerecord (4.0.4) lib/active_record/associations/association.rb:103:in `association_scope'
activerecord (4.0.4) lib/active_record/associations/association.rb:87:in `scope'
activerecord (4.0.4) lib/active_record/associations/collection_association.rb:382:in `scope'
activerecord (4.0.4) lib/active_record/associations/collection_proxy.rb:37:in `initialize'
activerecord (4.0.4) lib/active_record/relation/delegation.rb:78:in `new'
activerecord (4.0.4) lib/active_record/associations/collection_association.rb:37:in `reader'
activerecord (4.0.4) lib/active_record/associations/builder/association.rb:70:in `comment_images'
app/controllers/comments_controller.rb:18:in `new'
actionpack (4.0.4) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.0.4) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.0.4) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.0.4) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (4.0.4) lib/active_support/callbacks.rb:433:in `_run__1580909207748771124__process_action__callbacks'
activesupport (4.0.4) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.4) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (4.0.4) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.0.4) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.0.4) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.0.4) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.0.4) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.0.4) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.0.4) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.0.4) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.0.4) lib/abstract_controller/base.rb:136:in `process'
actionpack (4.0.4) lib/abstract_controller/rendering.rb:44:in `process'
actionpack (4.0.4) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.0.4) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.0.4) lib/action_controller/metal.rb:231:in `block in action'
actionpack (4.0.4) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
actionpack (4.0.4) lib/action_dispatch/routing/route_set.rb:48:in `call'
actionpack (4.0.4) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.0.4) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.0.4) lib/action_dispatch/routing/route_set.rb:674:in `call'
omniauth (1.2.1) lib/omniauth/strategy.rb:186:in `call!'
omniauth (1.2.1) lib/omniauth/strategy.rb:164:in `call'
omniauth (1.2.1) lib/omniauth/builder.rb:59:in `call'
meta_request (0.3.0) lib/meta_request/middlewares/app_request_handler.rb:13:in `call'
rack-contrib (1.1.0) lib/rack/contrib/response_headers.rb:17:in `call'
meta_request (0.3.0) lib/meta_request/middlewares/headers.rb:16:in `call'
meta_request (0.3.0) lib/meta_request/middlewares/meta_request_handler.rb:13:in `call'
newrelic_rpm (3.8.0.218) lib/new_relic/rack/error_collector.rb:55:in `call'
newrelic_rpm (3.8.0.218) lib/new_relic/rack/agent_hooks.rb:32:in `call'
newrelic_rpm (3.8.0.218) lib/new_relic/rack/browser_monitoring.rb:27:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.0.4) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.0.4) lib/action_dispatch/middleware/flash.rb:241:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.0.4) lib/action_dispatch/middleware/cookies.rb:486:in `call'
activerecord (4.0.4) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.0.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
activerecord (4.0.4) lib/active_record/migration.rb:373:in `call'
actionpack (4.0.4) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.0.4) lib/active_support/callbacks.rb:373:in `_run__2395107169417203351__call__callbacks'
activesupport (4.0.4) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.4) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.0.4) lib/action_dispatch/middleware/reloader.rb:64:in `call'
actionpack (4.0.4) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
better_errors (1.1.0) lib/better_errors/middleware.rb:84:in `protected_app_call'
better_errors (1.1.0) lib/better_errors/middleware.rb:79:in `better_errors_call'
better_errors (1.1.0) lib/better_errors/middleware.rb:56:in `call'
actionpack (4.0.4) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.0.4) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.0.4) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.4) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.0.4) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.0.4) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.0.4) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.0.4) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.0.4) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.0.4) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.0.4) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.0.4) lib/rails/engine.rb:511:in `call'
railties (4.0.4) lib/rails/application.rb:97:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/home/action/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
/home/action/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
/home/action/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'
The problem was with
attr_accessible :image, :attachable_id, :attachable_type
I had 'protected_attributes' gem, but I still had to create controller with strong_parameters instead to make it work. More info here.

Resources