I am getting ForbiddenAttrutesError every time I try and save the below form
.mdl-grid.mdl-cell.mdl-cell--6-col.mdl-cell--4-offset
.mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label
= form.text_field :name, class: 'mdl-textfield__input'
= form.label :name, class: 'mdl-textfield__label'
.mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label
= form.email_field :email, class: 'mdl-textfield__input'
= form.label :email, class: 'mdl-textfield__label'
= form.fields_for :addresses, Address.new do |address_fields|
.mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label
= address_fields.text_field :first_name, class: 'mdl-textfield__input'
= address_fields.label :first_name, class: 'mdl-textfield__label'
.mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label
= address_fields.text_field :last_name, class: 'mdl-textfield__input'
= address_fields.label :last_name, class: 'mdl-textfield__label'
.mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label
= address_fields.text_field :address1, class: 'mdl-textfield__input'
= address_fields.label :address1, class: 'mdl-textfield__label'
.mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label
= address_fields.text_field :address2, class: 'mdl-textfield__input'
= address_fields.label :address2, class: 'mdl-textfield__label'
.mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label
= address_fields.text_field :city, class: 'mdl-textfield__input'
= address_fields.label :city, class: 'mdl-textfield__label'
.mdl-textfield.mdl-js-textfield.mdl-textfield--floating-label
= address_fields.text_field :zip_code, class: 'mdl-textfield__input'
= address_fields.label :zip_code, class: 'mdl-textfield__label'
Address Model
class Address < ActiveRecord::Base
belongs_to :state
belongs_to :country
belongs_to :address_type
belongs_to :addressable, :polymorphic => true
Merchant Model
has_many :addresses, dependent: :destroy, as: :addressable
has_one :default_billing_address, -> { where(billing_default: true, active: true) },
as: :addressable,
class_name: 'Address'
has_many :billing_addresses, -> { where(active: true) },
as: :addressable,
class_name: 'Address'
has_one :default_shipping_address, -> { where(default: true, active: true) },
as: :addressable,
class_name: 'Address'
has_many :shipping_addresses, -> { where(active: true) },
as: :addressable,
class_name: 'Address'
before_validation :sanitize_data
validates :name, presence: true, length: { maximum: 255 }
validates :email, format: { with: CustomValidators::Emails.email_validator }, :length => { :maximum => 255 }
# geocoded_by :address
# after_validation :geocode
# after_create :sanitize_dates
accepts_nested_attributes_for :addresses
Merchant Controller
private
def allowed_params
params.require(:merchant).permit(:name, :email, addresses_attributes: [:first_name, :last_name, :address1, :address2, :city, :zip_code, :country_id])
end
Full Error Message
Started POST "/en/admin/merchants" for 127.0.0.1 at 2016-05-24 22:30:02 +1000
Processing by Admin::MerchantsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"[FILTERED]", "merchant"=>{"name"=>"Test", "email"=>"test#test.com", "addresses_attributes"=>{"0"=>{"first_name"=>"Paul", "last_name"=>"M", "address1"=>"12 Dansu Ct", "address2"=>"", "city"=>"Hallam", "zip_code"=>"3803"}}}, "commit"=>"Create", "locale"=>"en"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["remember_token", "5d0fec3c9ccb5afcd28845dd83eb18869f66b4b0"]]
Completed 500 Internal Server Error in 16ms (ActiveRecord: 0.4ms)
ActiveModel::ForbiddenAttributesError (ActiveModel::ForbiddenAttributesError):
activemodel (4.2.6) lib/active_model/forbidden_attributes_protection.rb:21:in `sanitize_for_mass_assignment'
activerecord (4.2.6) lib/active_record/attribute_assignment.rb:33:in `assign_attributes'
activerecord (4.2.6) lib/active_record/core.rb:566:in `init_attributes'
activerecord (4.2.6) lib/active_record/core.rb:281:in `initialize'
activerecord (4.2.6) lib/active_record/inheritance.rb:61:in `new'
activerecord (4.2.6) lib/active_record/inheritance.rb:61:in `new'
cancancan (1.14.0) lib/cancan/controller_resource.rb:80:in `build_resource'
cancancan (1.14.0) lib/cancan/controller_resource.rb:61:in `load_resource_instance'
cancancan (1.14.0) lib/cancan/controller_resource.rb:32:in `load_resource'
cancancan (1.14.0) lib/cancan/controller_resource.rb:25:in `load_and_authorize_resource'
cancancan (1.14.0) lib/cancan/controller_resource.rb:10:in `block in add_before_filter'
activesupport (4.2.6) lib/active_support/callbacks.rb:448:in `instance_exec'
activesupport (4.2.6) lib/active_support/callbacks.rb:448:in `block in make_lambda'
activesupport (4.2.6) lib/active_support/callbacks.rb:164:in `block in halting'
activesupport (4.2.6) lib/active_support/callbacks.rb:504:in `block in call'
activesupport (4.2.6) lib/active_support/callbacks.rb:504:in `each'
activesupport (4.2.6) lib/active_support/callbacks.rb:504:in `call'
activesupport (4.2.6) lib/active_support/callbacks.rb:92:in `__run_callbacks__'
activesupport (4.2.6) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
activesupport (4.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.6) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.6) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (4.2.6) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.6) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.6) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.6) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.6) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.6) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.6) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.6) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.2.6) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.6) lib/action_controller/metal.rb:237:in `block in action'
actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:74:in `dispatch'
actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:43:in `serve'
actionpack (4.2.6) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.6) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.6) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:817:in `call'
bullet (5.1.0) lib/bullet/rack.rb:10:in `call'
clearance (1.14.1) lib/clearance/rack_session.rb:23: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.6) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.6) 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.6) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.6) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.6) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
activesupport (4.2.6) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
activesupport (4.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.6) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:17: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:28: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 (4.2.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.6) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.6) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.6) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.6) 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.2.6) 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'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/static.rb:120:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.6) lib/rails/engine.rb:518:in `call'
railties (4.2.6) lib/rails/application.rb:165:in `call'
rack (1.6.4) lib/rack/content_length.rb:15: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'
Appears that 'cancancan' gem was interfering with the strong parameters causing the issues with the nested_forms. Since removing the gem it has resolved my issue.
Related
I am using Devise auth token gem for authenticating some parts of my rails app. But when I try to log in, I get the following error:
POST http://localhost:3000/auth/sign_in 500 (Internal Server Error)
Here is the complete trace:
Started POST "/auth/sign_in" for 10.12.4.5 at 2018-05-30 09:29:36
+0500 Processing by DeviseTokenAuth::SessionsController#create as JSON Parameters: {"login"=>"admin", "password"=>"[FILTERED]",
"session"=>{"login"=>"admin", "password"=>"[FILTERED]"}} Completed 500
Internal Server Error in 14ms (ActiveRecord: 0.0ms)
NoMethodError (undefined method `each' for
):
devise_token_auth (0.1.43)
app/controllers/devise_token_auth/application_controller.rb:35:in
params_for_resource' devise_token_auth (0.1.43)
app/controllers/devise_token_auth/sessions_controller.rb:129:in
resource_params' devise_token_auth (0.1.43)
app/controllers/devise_token_auth/sessions_controller.rb:13:in
create' actionpack (5.1.6)
lib/action_controller/metal/basic_implicit_render.rb:4:in
send_action' actionpack (5.1.6)
lib/abstract_controller/base.rb:186:in process_action' actionpack
(5.1.6) lib/action_controller/metal/rendering.rb:30:in
process_action' actionpack (5.1.6)
lib/abstract_controller/callbacks.rb:20:in block in process_action'
activesupport (5.1.6) lib/active_support/callbacks.rb:131:in
run_callbacks' actionpack (5.1.6)
lib/abstract_controller/callbacks.rb:19:in process_action' actionpack
(5.1.6) lib/action_controller/metal/rescue.rb:20:inprocess_action'
actionpack (5.1.6)
lib/action_controller/metal/instrumentation.rb:32:in block in
process_action' activesupport (5.1.6)
lib/active_support/notifications.rb:166:inblock in instrument'
activesupport (5.1.6)
lib/active_support/notifications/instrumenter.rb:21:in instrument'
activesupport (5.1.6) lib/active_support/notifications.rb:166:in
instrument' actionpack (5.1.6)
lib/action_controller/metal/instrumentation.rb:30:in process_action'
actionpack (5.1.6)
lib/action_controller/metal/params_wrapper.rb:252:inprocess_action'
activerecord (5.1.6)
lib/active_record/railties/controller_runtime.rb:22:in
process_action' actionpack (5.1.6)
lib/abstract_controller/base.rb:124:inprocess' actionpack (5.1.6)
lib/action_controller/metal.rb:189:in dispatch' actionpack (5.1.6)
lib/action_controller/metal.rb:253:indispatch' actionpack (5.1.6)
lib/action_dispatch/routing/route_set.rb:49:in dispatch' actionpack
(5.1.6) lib/action_dispatch/routing/route_set.rb:31:inserve'
actionpack (5.1.6) lib/action_dispatch/routing/mapper.rb:16:in block
in <class:Constraints>' actionpack (5.1.6)
lib/action_dispatch/routing/mapper.rb:46:inserve' actionpack (5.1.6)
lib/action_dispatch/journey/router.rb:50:in block in serve'
actionpack (5.1.6) lib/action_dispatch/journey/router.rb:33:ineach'
actionpack (5.1.6) lib/action_dispatch/journey/router.rb:33:in serve'
actionpack (5.1.6) lib/action_dispatch/routing/route_set.rb:844:in
call' warden (1.2.7) lib/warden/manager.rb:36:in block in call'
warden (1.2.7) lib/warden/manager.rb:35:incatch' warden (1.2.7)
lib/warden/manager.rb:35:in call' rack (2.0.5) lib/rack/etag.rb:25:in
call' rack (2.0.5) lib/rack/conditional_get.rb:38:in call' rack
(2.0.5) lib/rack/head.rb:12:incall' activerecord (5.1.6)
lib/active_record/migration.rb:556:in call' actionpack (5.1.6)
lib/action_dispatch/middleware/callbacks.rb:26:inblock in call'
activesupport (5.1.6) lib/active_support/callbacks.rb:97:in
run_callbacks' actionpack (5.1.6)
lib/action_dispatch/middleware/callbacks.rb:24:incall' actionpack
(5.1.6) lib/action_dispatch/middleware/executor.rb:12:in call'
actionpack (5.1.6)
lib/action_dispatch/middleware/debug_exceptions.rb:59:incall'
actionpack (5.1.6)
lib/action_dispatch/middleware/show_exceptions.rb:31:in call'
railties (5.1.6) lib/rails/rack/logger.rb:36:incall_app' railties
(5.1.6) lib/rails/rack/logger.rb:24:in block in call' activesupport
(5.1.6) lib/active_support/tagged_logging.rb:69:inblock in tagged'
activesupport (5.1.6) lib/active_support/tagged_logging.rb:26:in
tagged' activesupport (5.1.6)
lib/active_support/tagged_logging.rb:69:intagged' railties (5.1.6)
lib/rails/rack/logger.rb:24:in call' actionpack (5.1.6)
lib/action_dispatch/middleware/remote_ip.rb:79:incall' actionpack
(5.1.6) lib/action_dispatch/middleware/request_id.rb:25:in call' rack
(2.0.5) lib/rack/runtime.rb:22:incall' activesupport (5.1.6)
lib/active_support/cache/strategy/local_cache_middleware.rb:27:in
call' actionpack (5.1.6)
lib/action_dispatch/middleware/executor.rb:12:incall' actionpack
(5.1.6) lib/action_dispatch/middleware/static.rb:125:in call' rack
(2.0.5) lib/rack/sendfile.rb:111:incall' rack-cors (1.0.2)
lib/rack/cors.rb:97:in call' railties (5.1.6)
lib/rails/engine.rb:522:incall' puma (3.11.4)
lib/puma/configuration.rb:225:in call' puma (3.11.4)
lib/puma/server.rb:632:inhandle_request' puma (3.11.4)
lib/puma/server.rb:446:in process_client' puma (3.11.4)
lib/puma/server.rb:306:inblock in run' puma (3.11.4)
lib/puma/thread_pool.rb:120:in `block in spawn_thread'
How can I fix the error?
application_controller.rb:
class ApplicationController < ActionController::API
include DeviseTokenAuth::Concerns::SetUserByToken
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit(:login, :first_name, :patronymic, :last_name, :email, :password, :password_confirmation) }
devise_parameter_sanitizer.permit(:sign_in) { |u| u.permit(:login, :password) }
devise_parameter_sanitizer.permit(:account_update) { |u| u.permit(:password, :password_confirmation, :current_password) }
end
end
user.rb:
class User < ActiveRecord::Base
self.table_name = 'DASHBOARD.V_L_USERS'
self.primary_key = 'user_id'
devise :database_authenticatable, :validatable, :authentication_keys => [:login]
include DeviseTokenAuth::Concerns::User
has_many :permission_references, as: :source
accepts_nested_attributes_for :permission_references, :allow_destroy => true
has_many :user_roles
has_many :spr_dashboards, foreign_key: 'l_users_id'
accepts_nested_attributes_for :user_roles, :allow_destroy => true
def email_required?
false
end
def password_required?
false
end
def is_participant
(read_attribute(:is_participant) == "Y")
end
def is_participant=(value)
write_attribute(:is_participant, (value == "1") ? "Y" : "N")
end
def children_users_ids
ActiveRecord::Base.connection.select_values <<-SQL.strip_heredoc
SELECT l_users_id_right
FROM DASHBOARD.V_L_USERS_RIGHTS
WHERE l_users_id = #{user_id}
SQL
end
end
routes.rb:
Rails.application.routes.draw do
mount_devise_token_auth_for 'User', at: 'auth'
end
It looks like blocks are not supported in devise_parameter_sanitizer. https://github.com/lynndylanhurley/devise_token_auth/issues/758#issuecomment-280797969
I have had a similar situation where i had Activities which has one Task which has one taskable and building the task would also build the taskable, like so:
def new
#activity = Activity.new
#activity.task = #activity.build_task
end
Now i have Terms which have many TermTranslations which have many attachments which have an attachable (Image or Audio) but line 18 #term = Term.new(permitted_params[:term]) of admin/term.rb i get this error:
Cannot build association `attachable'. Are you trying to build a
polymorphic one-to-one association.
admin/term.rb
ActiveAdmin.register Term do
permit_params :id, :display_name, :slug,
term_translations_attributes: [:id, :slug, :title, :definition, :example, :language_id, :term_id, :_destroy,
attachments_attributes: [:id, :name, :attachable_type, :attachable_id, :_destroy,
attachable_attributes: [:id, :image_file, :audio_file, :_destroy]]]
form partial: 'form'
controller do
def new
#term = Term.new
#term.term_translations.build.attachments.build
end
def create
#term = Term.new(permitted_params[:term])
#term.term_translations.each do |tt|
tt.attachments.each do |a|
a.attachable = create_attachable(permitted_params[:term][:term_translations_attributes])
end
end
if #term.save
redirect_to admin_term_path(#term)
else
render :new
end
end
private
def create_attachable(term_translations_params)
raise term_translations_params.values[0].inspect
attachable = term_translations_params.values[0][:attachments_attributes].values[0][:attachable_type].classify
attachable.safe_constantize.new(term_translations_params.values[0][:attachable_attributes])
end
end
end
MODELS
class Term < ActiveRecord::Base
has_many :term_translations, dependent: :destroy
has_many :activities
accepts_nested_attributes_for :term_translations, allow_destroy: true
end
-
class TermTranslation < ActiveRecord::Base
belongs_to :language
belongs_to :term
has_many :attachments, dependent: :destroy
accepts_nested_attributes_for :attachments, allow_destroy: true
attr_accessor :attachable_attributes
end
-
class Attachment < ActiveRecord::Base
belongs_to :term_translation
belongs_to :attachable, polymorphic: true, dependent: :destroy
accepts_nested_attributes_for :attachable, allow_destroy: true
end
-
class Image < ActiveRecord::Base
has_attached_file :image_file, path: '/images/:filename'
validates_attachment_content_type :image_file, content_type: /\Aimage\/.*\Z/
has_one :attachment, as: :attachable
end
Views
admin/terms/_form.html.erb:
<%= semantic_form_for [:admin, #term], :builder => ActiveAdmin::FormBuilder, multipart: true do |f| %>
<%= f.semantic_errors *f.object.errors.keys %>
<%= f.inputs 'Details' do %>
<%= f.input :display_name %>
<%= f.input :slug %>
<% end %>
<%= render partial: '/admin/term_translations/form', locals: {f: f} %>
<%= f.actions %>
<% end %>
admin/term_translations/_form.html.erb:
<% f.has_many :term_translations, heading: 'Translations', allow_destroy: true, new_record: true, multipart: true do |t| %>
<% t.input :title %>
<% t.input :slug %>
<% t.input :language %>
<% t.has_many :attachments, heading: 'Attachments', allow_destroy: true, new_record: true, multipart: true do |a| %>
<% a.input :name %>
<% if a.object.attachable %>
<% a.input :attachable_type, as: :string, :input_html => { readonly: true } %>
<% else %>
<% a.input :attachable_type, as: :select, collection: ['Image', 'Audio'] %>
<% end %>
<div id='attachable_partial_container'>
<% if a.object.attachable.class.to_s == 'Image' %>
<%= render partial: '/admin/images/form', locals: { a: a } %>
<% elsif a.object.attachable.class.to_s == 'Audio' %>
<%= render partial: '/admin/audios/form', locals: { a: a } %>
<% end %>
</div>
<script type="text/javascript">
$(function () {
$('#term_term_translations_attributes_NEW_TERM_TRANSLATION_RECORD_attachments_attributes_NEW_ATTACHMENT_RECORD_attachable_type_input').change(onTypeSelected);
function onTypeSelected(e) {
if (e.val === 'Image')
$('#attachable_partial_container').replaceWith(renderImage());
else if (e.val === 'Audio')
$('#attachable_partial_container').replaceWith(renderAudio());
}
function renderImage() {
return (
"<div id='attachable_partial_container'>" +
"<%= escape_javascript render partial: '/admin/images/form', locals: {a: a} %>" +
"</div"
);
}
function renderAudio() {
return (
"<div id='attachable_partial_container'>" +
"<%= escape_javascript render partial: '/admin/audios/form', locals: {a: a} %>" +
"</div"
);
}
});
</script>
<% end %>
<% end %>
admin/images/_form.html.erb:
<%= a.inputs for: [:attachable_attributes, a.object.attachable || Image.new], multipart: true do |i| %>
<% i.input :image_file, as: :file, multipart: true %>
<% end %>
admin/audios/_form.html.erb:
<%= a.inputs for: [:attachable_attributes, a.object.attachable || Audio.new], multipart: true do |i| %>
<% i.input :audio_file, as: :file, multipart: true %>
<% end %>
Stack Trace
activerecord (4.2.0) lib/active_record/nested_attributes.rb:400:in `assign_nested_attributes_for_one_to_one_association'
activerecord (4.2.0) lib/active_record/nested_attributes.rb:343:in `attachable_attributes='
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:65:in `block in assign_nested_parameter_attributes'
activerecord (4.2.0) lib/active_record/attribute_assignment.rb:65:in `each'
activerecord (4.2.0) lib/active_record/attribute_assignment.rb:65:in `assign_nested_parameter_attributes'
activerecord (4.2.0) lib/active_record/attribute_assignment.rb:45: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/nested_attributes.rb:465:in `block in assign_nested_attributes_for_collection_association'
activerecord (4.2.0) lib/active_record/nested_attributes.rb:460:in `each'
activerecord (4.2.0) lib/active_record/nested_attributes.rb:460:in `assign_nested_attributes_for_collection_association'
activerecord (4.2.0) lib/active_record/nested_attributes.rb:343:in `attachments_attributes='
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:65:in `block in assign_nested_parameter_attributes'
activerecord (4.2.0) lib/active_record/attribute_assignment.rb:65:in `each'
activerecord (4.2.0) lib/active_record/attribute_assignment.rb:65:in `assign_nested_parameter_attributes'
activerecord (4.2.0) lib/active_record/attribute_assignment.rb:45: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/nested_attributes.rb:465:in `block in assign_nested_attributes_for_collection_association'
activerecord (4.2.0) lib/active_record/nested_attributes.rb:460:in `each'
activerecord (4.2.0) lib/active_record/nested_attributes.rb:460:in `assign_nested_attributes_for_collection_association'
activerecord (4.2.0) lib/active_record/nested_attributes.rb:343:in `term_translations_attributes='
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:65:in `block in assign_nested_parameter_attributes'
activerecord (4.2.0) lib/active_record/attribute_assignment.rb:65:in `each'
activerecord (4.2.0) lib/active_record/attribute_assignment.rb:65:in `assign_nested_parameter_attributes'
activerecord (4.2.0) lib/active_record/attribute_assignment.rb:45: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'
app/admin/term.rb:18: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:151:in `block in halting_and_conditional'
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 `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
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 `block in halting'
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 `_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 `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.7) lib/warden/manager.rb:36:in `block in call'
warden (1.2.7) lib/warden/manager.rb:35:in `catch'
warden (1.2.7) lib/warden/manager.rb:35:in `call'
rack (1.6.8) lib/rack/etag.rb:24:in `call'
rack (1.6.8) lib/rack/conditionalget.rb:38:in `call'
rack (1.6.8) 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.8) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.8) 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 `_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.3.0) lib/web_console/middleware.rb:28:in `block in call'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `catch'
web-console (2.3.0) lib/web_console/middleware.rb:18: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.8) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.8) 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.8) lib/rack/lock.rb:17:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/static.rb:113:in `call'
rack (1.6.8) 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.8) lib/rack/lock.rb:17:in `call'
rack (1.6.8) lib/rack/content_length.rb:15:in `call'
rack (1.6.8) lib/rack/handler/webrick.rb:88:in `service'
/Users/lancecoe/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/webrick/httpserver.rb:140:in `service'
/Users/lancecoe/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/webrick/httpserver.rb:96:in `run'
/Users/lancecoe/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/webrick/server.rb:296:in `block in start_thread'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"zehGpEWIuaFoAbIcOiDiaRiP7UAZxl999YuF79fPL9yypz55Jvo/QVEi3N6idELDTElORw2OoQMu3+2+CI63Pw==",
"term"=>{"display_name"=>"angle",
"slug"=>"angle",
"term_translations_attributes"=>{"0"=>{"title"=>"angle",
"slug"=>"angle",
"language_id"=>"1",
"attachments_attributes"=>{"0"=>{"name"=>"sss",
"attachable_type"=>"Image",
"attachable_attributes"=>{"image_file"=>#<ActionDispatch::Http::UploadedFile:0x007feaadf23358 #tempfile=#<Tempfile:/var/folders/xc/wjddz4j93hl7pl9d3z6ks5340000gn/T/RackMultipart20170619-14398-1ihr5sn.png>,
#original_filename="Screen Shot 2017-06-19 at 5.06.39 PM.png",
#content_type="image/png",
#headers="Content-Disposition: form-data; name=\"term[term_translations_attributes][0][attachments_attributes][0][attachable_attributes][image_file]\"; filename=\"Screen Shot 2017-06-19 at 5.06.39 PM.png\"\r\nContent-Type: image/png\r\n">}}}}}},
"commit"=>"Create Term"}
Just had to add this to the Attachment model
def build_attachable(params)
self.attachable = attachable_type.constantize.new(params)
end
I just switched my app from a hand-made login system to use the Devise gem. I have used this gem with many apps before, but have never had this issue. All the views are implemented correctly and display when they should, but when I try to create a new registration I get the following error:
ActionController::UnknownFormat in RegistrationsController#create
The trace it gives is as follows:
responders (2.2.0) lib/action_controller/respond_with.rb:207:in `respond_with'
devise (4.2.0) app/controllers/devise/registrations_controller.rb:32:in `create'
actionpack (4.2.6) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.2.6) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.6) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.6) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.6) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.6) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.6) lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
activesupport (4.2.6) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.6) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.6) lib/active_support/callbacks.rb:92:in `__run_callbacks__'
activesupport (4.2.6) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
activesupport (4.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.6) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.6) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (4.2.6) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.6) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.6) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.6) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.6) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.6) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.6) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.6) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.2.6) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.6) lib/action_controller/metal.rb:237:in `block in action'
actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:74:in `call'
actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:74:in `dispatch'
actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:43:in `serve'
actionpack (4.2.6) lib/action_dispatch/routing/mapper.rb:49:in `serve'
actionpack (4.2.6) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.6) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.6) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:817:in `call'
warden (1.2.6) lib/warden/manager.rb:35:in `block in call'
warden (1.2.6) lib/warden/manager.rb:34:in `catch'
warden (1.2.6) 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.6) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.6) 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.6) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.6) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
activerecord (4.2.6) lib/active_record/migration.rb:377:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.6) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
activesupport (4.2.6) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
activesupport (4.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.6) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.6) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.6) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.6) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.6) 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.2.6) 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.6) 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.6) lib/action_dispatch/middleware/static.rb:120:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.6) lib/rails/engine.rb:518:in `call'
railties (4.2.6) lib/rails/application.rb:165: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'
/Users/elizabethbayardelle/.rbenv/versions/2.2.4/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
/Users/elizabethbayardelle/.rbenv/versions/2.2.4/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
/Users/elizabethbayardelle/.rbenv/versions/2.2.4/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'
When I attempt to log in, I also get a blank white screen that just says "You need to sign in or sign up before continuing."
I think these errors are related, but as I have had no experience with this set of errors with Devise I'm not 100% sure. Can anyone help me figure out what's going wrong here?
ADDITIONAL INFORMATION
Here's the code for registrations#new:
<div class="hero-image-row">
<div class="hero-image-outer text-center">
<div class="hero-image-inner text-center">
<%= image_tag 'Background 17.jpg', class: "hero-image",alt: "Beautiful golf hole by the ocean" %>
</div> <!-- hero-image-inner -->
</div> <!-- hero-image-inner -->
</div> <!-- row -->
<div class="overlap-hero-image">
<div class="buffer-25"></div>
<div class="buffer-50"></div>
<div class="container">
<div class="container text-center col-xs-10 col-xs-push-1" style="background-color: rgba(235, 235, 235, 0.8) !important">
<h1 style="color: black">Sign Up</h1>
<%= simple_form_for(resource, as: resource_name, url: user_registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<div class="form-inputs text-left row">
<div class="col-sm-12">
<%= f.input :email, required: true, autofocus: true %>
</div>
<div class="col-sm-6">
<%= f.input :password, required: true, hint: ("#{#minimum_password_length} characters minimum" if #minimum_password_length) %>
</div>
<div class="col-sm-6">
<%= f.input :password_confirmation, required: true %>
</div>
</div>
<div class="form-actions">
<%= f.button :submit, "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
</div> <!-- columns box -->
</div> <!-- container -->
</div> <!-- overlap-tall-hero-image -->
Here's a supplemental registrations_controller for some added columns:
class RegistrationsController < Devise::RegistrationsController
private
def sign_up_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :gender, :birthday, :dom_hand, :sport, :g_years, :g_rounds, :g_practice, :handicap, :g_score, :admin)
end
def account_update_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_password, :gender, :birthday, :dom_hand, :sport, :g_years, :g_rounds, :g_practice, :handicap, :g_score, :admin)
end
end
And here's how it's reflected in my routes:
devise_for :users, :controllers => { registrations: 'registrations' }
It turned out that Devise autogenerated this for both registrations#new and sessions#new:
<%= simple_form_for(resource, as: resource_name, url: user_registration_path(resource_name)) do |f| %>
The error disappeared when I removed (resource_name) on each form.
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/
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 }