Failed to save the new associated preference has_one association - ruby-on-rails

I try to create a nested model form for the has_one preference. (i'm using Rails 4)
but i get the below error which i do not understand how to resolve. This happens in the case when a user has not completed the preference form (with validations for the attributes of preference in place)
terminal display of error
ActiveRecord::RecordNotSaved - Failed to save the new associated preference.:
activerecord (4.2.0) lib/active_record/associations/has_one_association.rb:43:in `block in replace'
activerecord (4.2.0) lib/active_record/associations/has_one_association.rb:98:in `block in transaction_if'
activerecord (4.2.0) lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
activerecord (4.2.0) lib/active_record/connection_adapters/abstract/transaction.rb:188:in `within_new_transaction'
activerecord (4.2.0) lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
activerecord (4.2.0) lib/active_record/transactions.rb:220:in `transaction'
activerecord (4.2.0) lib/active_record/associations/has_one_association.rb:98:in `transaction_if'
activerecord (4.2.0) lib/active_record/associations/has_one_association.rb:33:in `replace'
activerecord (4.2.0) lib/active_record/associations/singular_association.rb:17:in `writer'
activerecord (4.2.0) lib/active_record/associations/builder/association.rb:123:in `preference='
app/controllers/preferences_controller.rb:55: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: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: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:151:in `block in halting_and_conditional'
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:151:in `block in halting_and_conditional'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:308:in `block (2 levels) in halting'
public_activity (1.5.0) lib/public_activity/utility/store_controller.rb:25:in `store_controller_for_public_activity'
activesupport (4.2.0) lib/active_support/callbacks.rb:427:in `block in make_lambda'
activesupport (4.2.0) lib/active_support/callbacks.rb:307: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: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 `serve'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:802:in `call'
warden (1.2.4) lib/warden/manager.rb:35:in `block in call'
warden (1.2.4) 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 `_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'
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.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 `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'
quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
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/content_length.rb:15: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.9.1) lib/eventmachine.rb:193: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.6.4) lib/rack/handler/thin.rb:19:in `run'
rack (1.6.4) lib/rack/server.rb:286:in `start'
railties (4.2.0) lib/rails/commands/server.rb:80:in `start'
railties (4.2.0) lib/rails/commands/commands_tasks.rb:80:in `block in server'
railties (4.2.0) lib/rails/commands/commands_tasks.rb:75:in `server'
railties (4.2.0) lib/rails/commands/commands_tasks.rb:39:in `run_command!'
railties (4.2.0) lib/rails/commands.rb:17:in `<top (required)>'
bin/rails:9:in `<top (required)>'
spring (1.7.1) lib/spring/client/rails.rb:28:in `call'
spring (1.7.1) lib/spring/client/command.rb:7:in `call'
spring (1.7.1) lib/spring/client.rb:30:in `run'
spring (1.7.1) bin/spring:49:in `<top (required)>'
spring (1.7.1) lib/spring/binstub.rb:11:in `<top (required)>'
/Users/ARTLoe/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
bin/spring:13:in `<top (required)>'
bin/rails:3:in `<main>'
could one advise me where i am going wrong?
In my user, and preference model i have the following :
class User < ActiveRecord::Base
has_one :preference, dependent: :destroy
accepts_nested_attributes_for :preference
end
class Preference < ActiveRecord::Base
validates_presence_of :category_idealethnicity, presence: true, message: "can't be blank"
validates_presence_of :category_idealreligion, presence: true, message: "can't be blank"
belongs_to :user
has_and_belongs_to_many :category_idealethnicity, dependent: :destroy
has_and_belongs_to_many :category_idealreligion, dependent: :destroy
end
users_controller
class UsersController < ApplicationController
respond_to :html, :xml, :json
before_action :authenticate_user!, :set_user, only: [:show, :edit, :update, :destroy]
def index
...
end
def show
....
end
def edit
...
end
def update
...
end
private
def set_user
#user = User.friendly.find(params[:id])
end
def user_params
params.require(:user).permit(:email, :firstname, preference_attributes: [:town, category_idealreligion_ids: [], category_idealsmoke_ids: []])
end
end
preferences_controller
class PreferencesController < ApplicationController
before_action :authenticate_user!, :set_preference, only: [:show, :edit, :update, :destroy]
def index
#preferences = Preference.all
end
def show
...
end
def new
#users = User.random
#user = current_user
#preference = #user.build_preference
end
def edit
...
end
def create
#user = User.friendly.find(params[:user_id])
#user.preference = Preference.new(preference_params)
#preference = #user.preference
respond_to do |format|
if #preference.save
format.html { redirect_to #preference, notice: 'Preference was successfully created.' }
format.json { render :show, status: :created, location: #preference }
else
format.html { render :new }
format.json { render json: #preference.errors, status: :unprocessable_entity }
end
end
end
def update
...
end
def destroy
...
end
private
def set_preference
#preference = Preference.find(params[:id])
end
def preference_params
params.require(:preference).permit(:town, :user_id, category_idealethnicity_ids: [], category_idealreligion_ids: [])
end
end
routes
Rails.application.routes.draw do
devise_for :users
resources :users do
resources :preferences
end
end
preference form
<div>
<%= simple_form_for [#user, #preference] do |f| %>
<%= f.error_notification %>
<%= f.association :category_idealethnicity, collection: CategoryIdealethnicity.all, prompt: "select", label: false, as: :check_boxes %>
<%= f.association :category_idealreligion, collection: CategoryIdealreligion.all, prompt: "select", label: false, as: :check_boxes %>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
</div>

The Answer to the question: display of my create method was wrong. below is the correct code
def create
#preference = Preference.new(preference_params)
#preference.user = current_user
respond_to do |format|
if #preference.save
format.html { redirect_to #preference, notice: 'Preference was successfully created.' }
format.json { render :show, status: :created, location: #preference }
else
format.html { render :new }
format.json { render json: #preference.errors, status: :unprocessable_entity }
end
end
end

Your create method looks suspicious to me. Try the below instead
def create
#user = User.friendly.find(params[:user_id])
#preference = #user.preference.new(preference_params)
respond_to do |format|
if #preference.save
format.html { redirect_to #preference, notice: 'Preference was successfully created.' }
format.json { render :show, status: :created, location: #preference }
else
format.html { render :new }
format.json { render json: #preference.errors, status: :unprocessable_entity }
end
end
end

Related

TypeError - can't quote Rack::Session::SessionId

I am getting a mystifying error when trying to access Questions#Show. Anytime I redirect to the View, I get a 500 internal server error. According to my server trace, the issue is in the Questions controller in 'set_question'
Started GET "/questions/1" for 127.0.0.1 at 2020-02-01 18:57:34 +0100
Processing by QuestionsController#show as HTML
Parameters: {"id"=>"1"}
User Load (1.1ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
Question Load (3.5ms) SELECT `questions`.* FROM `questions` WHERE `questions`.`id` = 1 LIMIT 1
↳ app/controllers/questions_controller.rb:105:in `set_question'
Completed 500 Internal Server Error in 15ms (ActiveRecord: 4.6ms | Allocations: 3861)
TypeError - can't quote Rack::Session::SessionId:
However I am not quite sure what is wrong with it
Questions Controller
class QuestionsController < ApplicationController
before_action :set_question, only: [:show, :edit, :update, :destroy]
impressionist :actions => [:show], unique: [:session_hash]
def index
end
def show
#answers = #question.answers.includes(:user).page(params[:page]).per(5)
end
def new
#question = Question.new
end
def edit
authorize #question
end
def create
#question = Question.new(question_params)
respond_to do |format|
if #question.save
format.html { redirect_to #question, notice: 'You have successfully asked a question!' }
format.json { render :show, status: :created, location: #question }
else
format.html { render :new }
format.json { render json: #question.errors, status: :unprocessable_entity }
end
end
end
def update
authorize #question
respond_to do |format|
if #question.update(question_params)
format.html { redirect_to #question, notice: 'Question successfully updated.' }
format.json { render :show, status: :ok, location: #question }
else
format.html { render :edit }
format.json { render json: #question.errors, status: :unprocessable_entity }
end
end
end
def destroy
authorize #question
#question.destroy
respond_to do |format|
format.html { redirect_to #questions_url, notice: 'Question successfully deleted.' }
format.json { head :no_content }
end
end
private
def set_question
#question = Question.find(params[:id])
end
def question_params
params.require(:question).permit(:title, :body, :tag_list).merge(user_id: current_user.id)
end
end
Anyone familiar with this TypeError? I have tried Google and StackOverflow and haven't seen any hints
I have used virtually identical code for other models, however this is the only one which gives me a TypeError
Stack Trace
activerecord (6.0.2.1) lib/active_record/connection_adapters/abstract/quoting.rb:231:in `_quote'
activerecord (6.0.2.1) lib/active_record/connection_adapters/abstract/quoting.rb:18:in `quote'
activerecord (6.0.2.1) lib/arel/collectors/substitute_binds.rb:17:in `add_bind'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:703:in `visit_Arel_Nodes_BindParam'
activerecord (6.0.2.1) lib/arel/visitors/visitor.rb:31:in `visit'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:611:in `visit_Arel_Nodes_Equality'
activerecord (6.0.2.1) lib/arel/visitors/visitor.rb:31:in `visit'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:790:in `block in inject_join'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:788:in `each'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:788:in `each_with_index'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:788:in `inject_join'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:578:in `visit_Arel_Nodes_And'
activerecord (6.0.2.1) lib/arel/visitors/visitor.rb:31:in `visit'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:790:in `block in inject_join'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:788:in `each'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:788:in `each_with_index'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:788:in `inject_join'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:181:in `collect_nodes_for'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:161:in `visit_Arel_Nodes_SelectCore'
activerecord (6.0.2.1) lib/arel/visitors/mysql.rb:28:in `visit_Arel_Nodes_SelectCore'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:128:in `block in visit_Arel_Nodes_SelectStatement'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:127:in `each'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:127:in `inject'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:127:in `visit_Arel_Nodes_SelectStatement'
activerecord (6.0.2.1) lib/arel/visitors/mysql.rb:23:in `visit_Arel_Nodes_SelectStatement'
activerecord (6.0.2.1) lib/arel/visitors/visitor.rb:31:in `visit'
activerecord (6.0.2.1) lib/arel/visitors/visitor.rb:11:in `accept'
activerecord (6.0.2.1) lib/arel/visitors/to_sql.rb:18:in `compile'
activerecord (6.0.2.1) lib/active_record/connection_adapters/abstract/database_statements.rb:34:in `to_sql_and_binds'
activerecord (6.0.2.1) lib/active_record/connection_adapters/abstract/query_cache.rb:99:in `select_all'
activerecord (6.0.2.1) lib/active_record/connection_adapters/mysql/database_statements.rb:12:in `select_all'
activerecord (6.0.2.1) lib/active_record/connection_adapters/abstract/database_statements.rb:77:in `select_one'
activerecord (6.0.2.1) lib/active_record/relation/finder_methods.rb:317:in `block in exists?'
activerecord (6.0.2.1) lib/active_record/relation.rb:828:in `skip_query_cache_if_necessary'
activerecord (6.0.2.1) lib/active_record/relation/finder_methods.rb:317:in `exists?'
impressionist (1.6.1) app/controllers/impressionist_controller.rb:103:in `check_unique_impression?'
impressionist (1.6.1) app/controllers/impressionist_controller.rb:99:in `check_impression?'
impressionist (1.6.1) app/controllers/impressionist_controller.rb:94:in `unique?'
impressionist (1.6.1) app/controllers/impressionist_controller.rb:44:in `impressionist_subapp_filter'
impressionist (1.6.1) app/controllers/impressionist_controller.rb:7:in `block in impressionist'
activesupport (6.0.2.1) lib/active_support/callbacks.rb:429:in `instance_exec'
activesupport (6.0.2.1) lib/active_support/callbacks.rb:429:in `block in make_lambda'
activesupport (6.0.2.1) lib/active_support/callbacks.rb:201:in `block (2 levels) in halting'
actionpack (6.0.2.1) lib/abstract_controller/callbacks.rb:34:in `block (2 levels) in <module:Callbacks>'
activesupport (6.0.2.1) lib/active_support/callbacks.rb:202:in `block in halting'
activesupport (6.0.2.1) lib/active_support/callbacks.rb:514:in `block in invoke_before'
activesupport (6.0.2.1) lib/active_support/callbacks.rb:514:in `each'
activesupport (6.0.2.1) lib/active_support/callbacks.rb:514:in `invoke_before'
activesupport (6.0.2.1) lib/active_support/callbacks.rb:110:in `block in run_callbacks'
public_activity (1.6.4) lib/public_activity/utility/store_controller.rb:27:in `store_controller_for_public_activity'
activesupport (6.0.2.1) lib/active_support/callbacks.rb:121:in `block in run_callbacks'
activesupport (6.0.2.1) lib/active_support/callbacks.rb:139:in `run_callbacks'
actionpack (6.0.2.1) lib/abstract_controller/callbacks.rb:41:in `process_action'
actionpack (6.0.2.1) lib/action_controller/metal/rescue.rb:22:in `process_action'
actionpack (6.0.2.1) lib/action_controller/metal/instrumentation.rb:33:in `block in process_action'
activesupport (6.0.2.1) lib/active_support/notifications.rb:180:in `block in instrument'
activesupport (6.0.2.1) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (6.0.2.1) lib/active_support/notifications.rb:180:in `instrument'
actionpack (6.0.2.1) lib/action_controller/metal/instrumentation.rb:32:in `process_action'
actionpack (6.0.2.1) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
activerecord (6.0.2.1) lib/active_record/railties/controller_runtime.rb:27:in `process_action'
actionpack (6.0.2.1) lib/abstract_controller/base.rb:136:in `process'
actionview (6.0.2.1) lib/action_view/rendering.rb:39:in `process'
rack-mini-profiler (1.1.5) lib/mini_profiler/profiling_methods.rb:104:in `block in profile_method'
actionpack (6.0.2.1) lib/action_controller/metal.rb:191:in `dispatch'
actionpack (6.0.2.1) lib/action_controller/metal.rb:252:in `dispatch'
actionpack (6.0.2.1) lib/action_dispatch/routing/route_set.rb:51:in `dispatch'
actionpack (6.0.2.1) lib/action_dispatch/routing/route_set.rb:33:in `serve'
actionpack (6.0.2.1) lib/action_dispatch/journey/router.rb:49:in `block in serve'
actionpack (6.0.2.1) lib/action_dispatch/journey/router.rb:32:in `each'
actionpack (6.0.2.1) lib/action_dispatch/journey/router.rb:32:in `serve'
actionpack (6.0.2.1) lib/action_dispatch/routing/route_set.rb:837:in `call'
rack-pjax (1.1.0) lib/rack/pjax.rb:12:in `call'
bullet (6.1.0) lib/bullet/rack.rb:15:in `call'
remotipart (1.4.4) lib/remotipart/middleware.rb:32:in `call'
warden (1.2.8) lib/warden/manager.rb:36:in `block in call'
warden (1.2.8) lib/warden/manager.rb:34:in `catch'
warden (1.2.8) lib/warden/manager.rb:34:in `call'
rack (2.1.2) lib/rack/tempfile_reaper.rb:17:in `call'
rack (2.1.2) lib/rack/etag.rb:27:in `call'
rack (2.1.2) lib/rack/conditional_get.rb:27:in `call'
rack (2.1.2) lib/rack/head.rb:14:in `call'
actionpack (6.0.2.1) lib/action_dispatch/http/content_security_policy.rb:18:in `call'
rack (2.1.2) lib/rack/session/abstract/id.rb:269:in `context'
rack (2.1.2) lib/rack/session/abstract/id.rb:263:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/cookies.rb:648:in `call'
activerecord (6.0.2.1) lib/active_record/migration.rb:567:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
activesupport (6.0.2.1) lib/active_support/callbacks.rb:101:in `run_callbacks'
actionpack (6.0.2.1) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/actionable_exceptions.rb:17:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:32:in `call'
web-console (4.0.1) lib/web_console/middleware.rb:132:in `call_app'
web-console (4.0.1) lib/web_console/middleware.rb:28:in `block in call'
web-console (4.0.1) lib/web_console/middleware.rb:17:in `catch'
web-console (4.0.1) lib/web_console/middleware.rb:17:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (6.0.2.1) lib/rails/rack/logger.rb:38:in `call_app'
railties (6.0.2.1) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (6.0.2.1) lib/active_support/tagged_logging.rb:80:in `block in tagged'
activesupport (6.0.2.1) lib/active_support/tagged_logging.rb:28:in `tagged'
activesupport (6.0.2.1) lib/active_support/tagged_logging.rb:80:in `tagged'
railties (6.0.2.1) lib/rails/rack/logger.rb:26:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/request_id.rb:27:in `call'
rack (2.1.2) lib/rack/method_override.rb:24:in `call'
rack (2.1.2) lib/rack/runtime.rb:24:in `call'
activesupport (6.0.2.1) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/static.rb:126:in `call'
rack (2.1.2) lib/rack/sendfile.rb:113:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/host_authorization.rb:83:in `call'
rack-mini-profiler (1.1.5) lib/mini_profiler/profiler.rb:296:in `call'
webpacker (4.2.2) lib/webpacker/dev_server_proxy.rb:23:in `perform_request'
rack-proxy (0.6.5) lib/rack/proxy.rb:57:in `call'
railties (6.0.2.1) lib/rails/engine.rb:526:in `call'
puma (4.3.1) lib/puma/configuration.rb:228:in `call'
puma (4.3.1) lib/puma/server.rb:681:in `handle_request'
puma (4.3.1) lib/puma/server.rb:472:in `process_client'
puma (4.3.1) lib/puma/server.rb:328:in `block in run'
puma (4.3.1) lib/puma/thread_pool.rb:134:in `block in spawn_thread'
I succeeded in reproducing the same exception. Perhaps your code or the gem you're using is running a query with session.id.
Solution
If your code uses session.id to identify a user, you should convert it to a String before using it.
# Bad
user = User.find_by(session_id: session.id)
# => TypeError can't quote Rack::Session::SessionId
# Good
user = User.find_by(session_id: session.id.to_s)
# Success!
If your code doesn't run a query by session.id, you should be suspicious of gems that insert processing in the middle of a rack session.
Steps to debug
This code is running in Rails 5.2.4.2.
# In controller
def new
logger.info "class=#{session.id.class} to_s=#{session.id.to_s}"
end
# The log for the above code
# INFO -- class=Rack::Session::SessionId to_s=adf8...9af0 <- a long string
If you run a query by session.id, you get the TypeError.
SomethingModel.where(session_id: session.id).to_a
# => TypeError can't quote Rack::Session::SessionId
Replacing the following code in config/initializers/session_store.rb:
Rails.application.config.session_store :active_record_store, :key => '....'
with
Rails.application.config.action_dispatch.session_store :active_record_store, :key => '....'
fixed the TypeError can't quote Rack::Session::SessionId issue for me.
Note: make sure to replace the '....' with your own key.
Apparently, there is nothing wrong with your set_question method. The error says: TypeError - can't quote Rack::Session::SessionId: and that is happening in impressionist :actions => [:show], unique: [:session_hash].
I haven't used this gem so can't exactly tell you how to counter that error but to prove my point, if you comment this line (line 3) in your controller, it will work.
You need to read more about implementing this gem to resolve it.
The session hash is string in impressionist but our session is stored with id if we are using device. Not sure with this impressionist gem but following solved my problem:
impressionist :actions => [:show], unique: [:impressionable_type,:impressionable_id,:session_hash.to_s]

How can I correct a 'SystemStackError (stack level too deep)' on HABTM relationship?

I'm trying to debug/resolve an error I'm getting when trying to add to an association on my report model. I keep getting a SystemStackError (stack level too deep) error that I've read is likely due to an infinite loop call -- but I can't seem to locate it. Here's my models and the controller. I've narrowed it down to when I try to add "Alerts" to a "Report". If I need to include more please let me know. Thanks!
Report
class Report < ActiveRecord::Base
self.inheritance_column = :disabled
# Associations
has_and_belongs_to_many :alerts
accepts_nested_attributes_for :alerts, allow_destroy: true, reject_if: :all_blank
validates_uniqueness_of :author_id, scope: :player_id
public
def all_alerts
alerts_string = ""
alerts.order(letter: :asc).each do |a|
alerts_string += a.letter.upcase
end
alerts_string
end
end
Alert
class Alert < ActiveRecord::Base
self.inheritance_column = :disabled
has_and_belongs_to_many :reports
public
def to_s
"#{letter.upcase}"
end
end
Players::ReportsController
class Players::ReportsController < ApplicationController
# ... other actions
def update
respond_to do |format|
if #report.update(report_params)
format.html { redirect_to player_report_path(#report.player, #report), notice: 'Report was successfully updated.' }
format.json { render :show, status: :ok, location: player_report_path(#report.player, #report) }
else
format.html { render :edit }
format.json { render json: #report.errors, status: :unprocessable_entity }
end
end
end
def destroy
#report.destroy
respond_to do |format|
format.html { redirect_to player_reports_path(#report.player), notice: 'Report was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Never trust parameters from the scary internet, only allow the white list through.
def report_params
params.require(:report).permit(:author_id, :player_id, { alert_ids:[] })
end
end
Edit: Added Stack Trace
bullet (4.14.9) lib/bullet/active_record42.rb:194:in `has_cached_counter?'
bullet (4.14.9) lib/bullet/active_record42.rb:194:in `has_cached_counter?'
bullet (4.14.9) lib/bullet/active_record42.rb:194:in `has_cached_counter?'
bullet (4.14.9) lib/bullet/active_record42.rb:194:in `has_cached_counter?'
bullet (4.14.9) lib/bullet/active_record42.rb:194:in `has_cached_counter?'
bullet (4.14.9) lib/bullet/active_record42.rb:205:in `has_cached_counter?'
activerecord (4.2.0) lib/active_record/associations/has_many_through_association.rb:67:in `insert_record'
activerecord (4.2.0) lib/active_record/associations/collection_association.rb:559:in `block (2 levels) in concat_records'
activerecord (4.2.0) lib/active_record/associations/collection_association.rb:398:in `replace_on_target'
activerecord (4.2.0) lib/active_record/associations/collection_association.rb:393:in `add_to_target'
activerecord (4.2.0) lib/active_record/associations/collection_association.rb:558:in `block in concat_records'
activerecord (4.2.0) lib/active_record/associations/collection_association.rb:556:in `each'
activerecord (4.2.0) lib/active_record/associations/collection_association.rb:556:in `concat_records'
activerecord (4.2.0) lib/active_record/associations/has_many_association.rb:165:in `concat_records'
activerecord (4.2.0) lib/active_record/associations/has_many_through_association.rb:44:in `concat_records'
activerecord (4.2.0) lib/active_record/associations/collection_association.rb:158:in `block in concat'
activerecord (4.2.0) lib/active_record/associations/collection_association.rb:173:in `block in transaction'
activerecord (4.2.0) lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `transaction'
activerecord (4.2.0) lib/active_record/transactions.rb:220:in `transaction'
activerecord (4.2.0) lib/active_record/associations/collection_association.rb:172:in `transaction'
activerecord (4.2.0) lib/active_record/associations/collection_association.rb:158:in `concat'
activerecord (4.2.0) lib/active_record/associations/has_many_through_association.rb:38:in `concat'
activerecord (4.2.0) lib/active_record/associations/collection_association.rb:536:in `replace_records'
activerecord (4.2.0) lib/active_record/associations/collection_association.rb:363:in `block in replace'
activerecord (4.2.0) lib/active_record/associations/collection_association.rb:173:in `block in transaction'
activerecord (4.2.0) lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `transaction'
activerecord (4.2.0) lib/active_record/transactions.rb:220:in `transaction'
activerecord (4.2.0) lib/active_record/associations/collection_association.rb:172:in `transaction'
activerecord (4.2.0) lib/active_record/associations/collection_association.rb:363:in `replace'
activerecord (4.2.0) lib/active_record/associations/collection_association.rb:67:in `ids_writer'
activerecord (4.2.0) lib/active_record/associations/builder/collection_association.rb:76:in `alert_ids='
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/persistence.rb:249:in `block in update'
activerecord (4.2.0) lib/active_record/transactions.rb:347:in `block in with_transaction_returning_status'
activerecord (4.2.0) lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
activerecord (4.2.0) lib/active_record/connection_adapters/abstract/transaction.rb:188:in `within_new_transaction'
activerecord (4.2.0) lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
activerecord (4.2.0) lib/active_record/transactions.rb:220:in `transaction'
activerecord (4.2.0) lib/active_record/transactions.rb:344:in `with_transaction_returning_status'
activerecord (4.2.0) lib/active_record/persistence.rb:248:in `update'
app/controllers/players/reports_controller.rb:67:in `block in update'
actionpack (4.2.0) lib/action_controller/metal/mime_responds.rb:211:in `respond_to'
app/controllers/players/reports_controller.rb:66:in `update'
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:151:in `block in halting_and_conditional'
activesupport (4.2.0) lib/active_support/callbacks.rb:151: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:151: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:151: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: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:151: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 `call'
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 `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:308:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:308:in `block (2 levels) in halting'
rails-observers (0.1.2) lib/rails/observers/action_controller/caching/sweeping.rb:73:in `around'
activesupport (4.2.0) lib/active_support/callbacks.rb:450:in `public_send'
activesupport (4.2.0) lib/active_support/callbacks.rb:450:in `block in make_lambda'
activesupport (4.2.0) lib/active_support/callbacks.rb:307:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:307:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:151: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 `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'
bullet (4.14.9) lib/bullet/rack.rb:12: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:31: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'
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/daveomcd/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
/home/daveomcd/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
/home/daveomcd/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'
The top line bullet (4.14.9) lib/bullet/active_record42.rb:194:inhas_cached_counter?'` occurs 50+ times.
It seems like a possible Bullet problem to me, maybe You should post it as an issue on their Github? Bullet has a piece of code in it's lib/acive_record42.rb file:
alias_method :origin_has_cached_counter?, :has_cached_counter?
def has_cached_counter?(reflection = reflection())
result = origin_has_cached_counter?(reflection)
...
end
Looks like a place that might be generating SLTD, but there is a huge possibility that I am wrong.

Carrierwave Gem cannot upload file in Rails

I have set up Carrierwave to upload pdf files to my datebase. I used my Gemfile to install after running bundle install. The problem is that when I try to create a new Object in the database, I get an error:
TypeError in BulletinsController#create
can't cast ActionDispatch::Http::UploadedFile to string
I believe I set everything up correctly but I would appreciate any advice that could be given. Here are my classes/models/controllers.
AttachmentUploader:
class AttachmentUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
Bulletin Model:
class Bulletin < ActiveRecord::Base
mount_uploader :attachment, AttachmentUploader
end
Bulletins Controller:
class BulletinsController < ApplicationController
before_action :set_bulletin, only: [:show, :edit, :update, :destroy]
# GET /bulletins
# GET /bulletins.json
def index
#bulletins = Bulletin.all
end
# GET /bulletins/1
# GET /bulletins/1.json
def show
end
# GET /bulletins/new
def new
#bulletin = Bulletin.new
end
# GET /bulletins/1/edit
def edit
end
# POST /bulletins
# POST /bulletins.json
def create
#bulletin = Bulletin.new(bulletin_params)
respond_to do |format|
if #bulletin.save
format.html { redirect_to #bulletin, notice: 'Bulletin was successfully created.' }
format.json { render :show, status: :created, location: #bulletin }
else
format.html { render :new }
format.json { render json: #bulletin.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /bulletins/1
# PATCH/PUT /bulletins/1.json
def update
respond_to do |format|
if #bulletin.update(bulletin_params)
format.html { redirect_to #bulletin, notice: 'Bulletin was successfully updated.' }
format.json { render :show, status: :ok, location: #bulletin }
else
format.html { render :edit }
format.json { render json: #bulletin.errors, status: :unprocessable_entity }
end
end
end
# DELETE /bulletins/1
# DELETE /bulletins/1.json
def destroy
#bulletin.destroy
respond_to do |format|
format.html { redirect_to bulletins_url, notice: 'Bulletin was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_bulletin
#bulletin = Bulletin.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def bulletin_params
params.require(:bulletin).permit(:title, :text, :attachment)
end
end
And finally the bulletin new.html form:
<%= form_for #bulletin, :html => {:multipart => true} do |f| %>
<% if #bulletin.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#bulletin.errors.count, "error") %> prohibited this bulletin from being saved:</h2>
<ul>
<% #bulletin.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :text %><br>
<%= f.text_area :text %>
</div>
<div class="field">
<%= f.label :attachment %><br>
<%= f.file_field :attachment %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
FULL TRACE:
activerecord (4.1.1) lib/active_record/connection_adapters/abstract/quoting.rb:76:in `type_cast'
activerecord (4.1.1) lib/active_record/connection_adapters/sqlite3_adapter.rb:261:in `type_cast'
activerecord (4.1.1) lib/active_record/connection_adapters/sqlite3_adapter.rb:295:in `block in exec_query'
activerecord (4.1.1) lib/active_record/connection_adapters/sqlite3_adapter.rb:294:in `map'
activerecord (4.1.1) lib/active_record/connection_adapters/sqlite3_adapter.rb:294:in `exec_query'
activerecord (4.1.1) lib/active_record/connection_adapters/abstract/database_statements.rb:78:in `exec_insert'
activerecord (4.1.1) lib/active_record/connection_adapters/abstract/database_statements.rb:105:in `insert'
activerecord (4.1.1) lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `insert'
activerecord (4.1.1) lib/active_record/relation.rb:64:in `insert'
activerecord (4.1.1) lib/active_record/persistence.rb:502:in `create_record'
activerecord (4.1.1) lib/active_record/attribute_methods/dirty.rb:87:in `create_record'
activerecord (4.1.1) lib/active_record/callbacks.rb:306:in `block in create_record'
activesupport (4.1.1) lib/active_support/callbacks.rb:82:in `run_callbacks'
activerecord (4.1.1) lib/active_record/callbacks.rb:306:in `create_record'
activerecord (4.1.1) lib/active_record/timestamp.rb:57:in `create_record'
activerecord (4.1.1) lib/active_record/persistence.rb:482:in `create_or_update'
activerecord (4.1.1) lib/active_record/callbacks.rb:302:in `block in create_or_update'
activesupport (4.1.1) lib/active_support/callbacks.rb:82:in `run_callbacks'
activerecord (4.1.1) lib/active_record/callbacks.rb:302:in `create_or_update'
activerecord (4.1.1) lib/active_record/persistence.rb:103:in `save'
activerecord (4.1.1) lib/active_record/validations.rb:51:in `save'
activerecord (4.1.1) lib/active_record/attribute_methods/dirty.rb:21:in `save'
activerecord (4.1.1) lib/active_record/transactions.rb:268:in `block (2 levels) in save'
activerecord (4.1.1) lib/active_record/transactions.rb:329:in `block in with_transaction_returning_status'
activerecord (4.1.1) lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `block in transaction'
activerecord (4.1.1) lib/active_record/connection_adapters/abstract/database_statements.rb:219:in `within_new_transaction'
activerecord (4.1.1) lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `transaction'
activerecord (4.1.1) lib/active_record/transactions.rb:208:in `transaction'
activerecord (4.1.1) lib/active_record/transactions.rb:326:in `with_transaction_returning_status'
activerecord (4.1.1) lib/active_record/transactions.rb:268:in `block in save'
activerecord (4.1.1) lib/active_record/transactions.rb:283:in `rollback_active_record_state!'
activerecord (4.1.1) lib/active_record/transactions.rb:267:in `save'
app/controllers/bulletins_controller.rb:30:in `block in create'
actionpack (4.1.1) lib/action_controller/metal/mime_responds.rb:433:in `call'
actionpack (4.1.1) lib/action_controller/metal/mime_responds.rb:433:in `retrieve_collector_from_mimes'
actionpack (4.1.1) lib/action_controller/metal/mime_responds.rb:256:in `respond_to'
app/controllers/bulletins_controller.rb:29:in `create'
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:149:in `block in halting_and_conditional'
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: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:35: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'
/usr/local/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/usr/local/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/usr/local/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"hCh2yJkRxNKW+z2ZOIISuAWu/2gRI++noacQxDnst78=",
"bulletin"=>{"title"=>"Test",
"text"=>"Testing123",
"attachment"=>#<ActionDispatch::Http::UploadedFile:0xc69d9b0 #tempfile=#<Tempfile:/tmp/RackMultipart20140619-13131-17crugy>,
#original_filename="test.pdf",
#content_type="application/pdf",
#headers="Content-Disposition: form-data; name=\"bulletin[attachment]\"; filename=\"test.pdf\"\r\nContent-Type: application/pdf\r\n">},
"commit"=>"Create Bulletin"

error when updating whilst using acts-as-taggable

Good Morning everyone,
I've been stumped with this error for a good days or two now.
My app is a small "Sample Request Log" for my textiles based company, it just needs to track samples and be able to edit/submit/delete them.
Whenever I try to update or create a new request I get a "undefined method `each' for "x":String" Where X is the option chosen in the drop down list (The drop down list is populated from another controller.)
Request_controller.rb:
class RequestsController < ApplicationController
before_action :set_request, only: [:show, :edit, :update, :destroy]
# GET /requests
# GET /requests.json
def index
if params[:tag]
#requests = Request.tagged_with(params[:tag])
else
#requests = Request.all
#requests = Request.order("request_date ASC")
end
end
# GET /requests/1
# GET /requests/1.json
def show
end
# GET /requests/new
def
new
#request = Request.new
#customers = Customer.all
#suppliers = Supplier.all
#designers = Designer.all
#statuses = Status.all
end
# GET /requests/1/edit
def edit
#customers = Customer.all
#suppliers = Supplier.all
#designers = Designer.all
#statuses = Status.all
end
# POST /requests
# POST /requests.json
def create
#request = Request.new(request_params)
#customers = Customer.all
#suppliers = Supplier.all
#designers = Designer.all
#statuses = Status.all
respond_to do |format|
if #request.save
format.html { redirect_to #request, notice: 'Request was successfully created.' }
format.json { render action: 'show', status: :created, location: #request }
else
format.html { render action: 'new' }
format.json { render json: #request.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /requests/1
# PATCH/PUT /requests/1.json
def update
#customers = Customer.all
#suppliers = Supplier.all
#designers = Designer.all
#statuses = Status.all
respond_to do |format|
if #request.update(request_params)
format.html { redirect_to #request, notice: 'Request was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #request.errors, status: :unprocessable_entity }
end
end
end
# DELETE /requests/1
# DELETE /requests/1.json
def destroy
#request.destroy
respond_to do |format|
format.html { redirect_to requests_url }
format.json { head :no_content }
end
end
def tagged
if params[:tags].present?
#requests = Request.tagged_with(params[:tags])
else
#requests = Request.postall
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_request
#request = Request.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def request_params
params.require(:request).permit(:request_date, :request_smd_number, :request_brief_description, :request_description, :request_supplier, :request_colours, :request_date_required, :request_required_for, :request_designer, :request_comments, :request_confirmed_comp_date, :request_received, :request_received_date, :request_date_sent, :request_tracking_number, :request_confirmed_comp_date, :request_confirmed_comp_date2, :request_confirmed_comp_date3, :request_confirmed_comp_date4, :request_confirmed_comp_date5, :request_confirmed_comp_date6, :tag_list, :tags, :supplier, :customer, :designer, :name)
end
end
Requests.rb
class Request < ActiveRecord::Base
validates_presence_of :request_smd_number, :request_date, :request_date_required,
:request_required_for, :request_brief_description, :request_comments, :tags,
:request_supplier
belongs_to :customer
belongs_to :designer
belongs_to :supplier
belongs_to :status
has_many :tags
accepts_nested_attributes_for :customer
accepts_nested_attributes_for :designer
accepts_nested_attributes_for :supplier
accepts_nested_attributes_for :status
acts_as_taggable_on :requests, :tags, :tag_list
end
The form I'm using can be found at https://github.com/jackybigz/samplelog/blob/master/app/views/requests/_form.html.erb (linking to git instead of pasting it in to keep the thread small.)
Whenever I take out the last line of the form '
<td class="span3"><%= collection_select :request, :tags, #statuses, :name, :name %></td>'
it works fine and updates the request so I've narrowed it down to that, but after shifting a bunch of relations and other things around, trying updates and different gem versions I've just come against a rock and a hard place.
I'm reasonably new to ruby and rails so any help and suggestions will be helpful, thanks.
P.S I've probably gone a completely different way with my apps code than I should of but it kind of works so it's how I did it.
EDIT here is the trace for the error.
activerecord (4.0.0) lib/active_record/associations/collection_association.rb:333:in `replace'
activerecord (4.0.0) lib/active_record/associations/collection_association.rb:42:in `writer'
activerecord (4.0.0) lib/active_record/associations/builder/association.rb:78:in `tags='
activerecord (4.0.0) lib/active_record/attribute_assignment.rb:42:in `public_send'
activerecord (4.0.0) lib/active_record/attribute_assignment.rb:42:in `_assign_attribute'
activerecord (4.0.0) lib/active_record/attribute_assignment.rb:29:in `block in assign_attributes'
activerecord (4.0.0) lib/active_record/attribute_assignment.rb:23:in `each'
activerecord (4.0.0) lib/active_record/attribute_assignment.rb:23:in `assign_attributes'
activerecord (4.0.0) lib/active_record/persistence.rb:229:in `block in update'
activerecord (4.0.0) lib/active_record/transactions.rb:326:in `block in with_transaction_returning_status'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
activerecord (4.0.0) lib/active_record/transactions.rb:209:in `transaction'
activerecord (4.0.0) lib/active_record/transactions.rb:323:in `with_transaction_returning_status'
activerecord (4.0.0) lib/active_record/persistence.rb:228:in `update'
app/controllers/requests_controller.rb:67:in `block in update'
actionpack (4.0.0) lib/action_controller/metal/mime_responds.rb:363:in `call'
actionpack (4.0.0) lib/action_controller/metal/mime_responds.rb:363:in `retrieve_collector_from_mimes'
actionpack (4.0.0) lib/action_controller/metal/mime_responds.rb:189:in `respond_to'
app/controllers/requests_controller.rb:66:in `update'
actionpack (4.0.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.0.0) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.0.0) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (4.0.0) lib/active_support/callbacks.rb:413:in `_run__1386668850689043198__process_action__callbacks'
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.0) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.0.0) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.0.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.0.0) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
activerecord (4.0.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.0.0) lib/abstract_controller/base.rb:136:in `process'
actionpack (4.0.0) lib/abstract_controller/rendering.rb:44:in `process'
actionpack (4.0.0) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.0.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.0.0) lib/action_controller/metal.rb:231:in `block in action'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:80:in `call'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:48:in `call'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:655: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.0.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.0.0) 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.0) lib/action_dispatch/middleware/cookies.rb:486:in `call'
activerecord (4.0.0) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
activerecord (4.0.0) lib/active_record/migration.rb:369:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__1572239184247043471__call__callbacks'
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'
actionpack (4.0.0) 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.0) 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.0) lib/action_dispatch/middleware/static.rb:64:in `call'
railties (4.0.0) lib/rails/engine.rb:511:in `call'
railties (4.0.0) lib/rails/application.rb:97:in `call'
rack (1.5.2) 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 `catch'
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.3) lib/eventmachine.rb:187:in `run_machine'
eventmachine (1.0.3) 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.2) lib/rack/handler/thin.rb:16:in `run'
rack (1.5.2) lib/rack/server.rb:264:in `start'
railties (4.0.0) lib/rails/commands/server.rb:84:in `start'
railties (4.0.0) lib/rails/commands.rb:78:in `block in <top (required)>'
railties (4.0.0) lib/rails/commands.rb:73:in `tap'
railties (4.0.0) lib/rails/commands.rb:73:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
I found the answer this morning, came at with a fresh head and just kind of got it.
<td class="span3"><%= collection_select :request, :tags, #statuses, :name, :name %></td>
replaced :tags with :tag_list in the collection select and it works now.

state_machine gem integration with rails 4

I am facing a problem with usage of state_machine gem with my rails 4 app.
A have a model which include a column call state as described in the provided rails tutorial
http://gistflow.com/posts/679-state-machine-with-rails-basics
but when I define my state_machine as follows:
class Issue < ActiveRecord::Base
validates :title, :description, presence: true
has_many :notes
state_machine :initial => :new do
state :new, value: 0
state :analysed, value: 1
state :assigned, value: 2
state :inprogress, value: 3
state :inreview, value: 4
state :validation, value: 5
state :resolved, value: 6
state :cancelled, value: 7
state :closed, value: 8
state :rejected, value: 9
state :reopened, value: 10
end
def next
Issue.where("id > ?", self.id).first || Issue.first
end
def prev
Issue.where("id < ?", self.id).last || Issue.last
end
end
Here is my IssueConntroller
class IssuesController < ApplicationController
before_filter :find_issue, except: [:index, :new, :create]
def index
#issues = Issue.all
end
def new
#issue = Issue.new
end
def create
#issue = Issue.new(issue_params)
respond_to do |format|
if #issue.save
format.html {redirect_to #issue, :flash => { :success => "Issue succesfully created." } }
format.json {head :no_content}
else
format.html {render action: 'new'}
format.json { render json: #issue.errors, status: :unprocessable_entity }
end
end
end
def show
#note = Note.new
#note.issue_id = #issue.id
end
def edit
end
def update
respond_to do |format|
if #issue.update(issue_params)
format.html { redirect_to #issue, :flash => { :success => 'Issue was successfully updated.' }}
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #issue.errors, status: :unprocessable_entity }
end
end
end
def destroy
#issue.destroy
flash[:error]= "Issue '#{#issue.title}' Deleted!"
redirect_to issues_path
end
private
def issue_params
params.require(:issue).permit(:title, :description)
end
def find_issue
#issue = Issue.find(params[:id])
end
end
I am falling into a undefined method `state_machine' for # NoMethodError in IssuesController#index.
of course I have added gem 'state_machine line into my gemfile and run bundle install command.
It seems like the gem is not loaded by the rails application...
Thank you for helping me out with this problem ;)
here is the trace
activerecord (4.0.0) lib/active_record/dynamic_matchers.rb:22:in `method_missing'
app/models/issue.rb:5:in `<class:Issue>'
app/models/issue.rb:1:in `<top (required)>'
activesupport (4.0.0) lib/active_support/dependencies.rb:423:in `load'
activesupport (4.0.0) lib/active_support/dependencies.rb:423:in `block in load_file'
activesupport (4.0.0) lib/active_support/dependencies.rb:615:in `new_constants_in'
activesupport (4.0.0) lib/active_support/dependencies.rb:422:in `load_file'
activesupport (4.0.0) lib/active_support/dependencies.rb:323:in `require_or_load'
activesupport (4.0.0) lib/active_support/dependencies.rb:462:in `load_missing_constant'
activesupport (4.0.0) lib/active_support/dependencies.rb:183:in `const_missing'
activesupport (4.0.0) lib/active_support/dependencies.rb:494:in `load_missing_constant'
activesupport (4.0.0) lib/active_support/dependencies.rb:183:in `const_missing'
app/controllers/issues_controller.rb:6:in `index'
actionpack (4.0.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.0.0) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.0.0) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (4.0.0) lib/active_support/callbacks.rb:413:in `_run__901873103026712443__process_action__callbacks'
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.0) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.0.0) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.0.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.0.0) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
activerecord (4.0.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.0.0) lib/abstract_controller/base.rb:136:in `process'
actionpack (4.0.0) lib/abstract_controller/rendering.rb:44:in `process'
actionpack (4.0.0) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.0.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.0.0) lib/action_controller/metal.rb:231:in `block in action'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:80:in `call'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:48:in `call'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:655: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.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.0.0) 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.0) lib/action_dispatch/middleware/cookies.rb:486:in `call'
activerecord (4.0.0) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
activerecord (4.0.0) lib/active_record/migration.rb:369:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__77618213725076532__call__callbacks'
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.0.0) li/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/Users/sebmac/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
/Users/sebmac/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
/Users/sebmac/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'b/action_dispatch/middleware/static.rb:64:in `call'
railties (4.0.0) lib/rails/engine.rb:511:in `call'
railties (4.0.0) 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'
/Users/sebmac/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
/Users/sebmac/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
/Users/sebmac/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'
There is no problems in you code, try to reload SERVER after bundle , don't forget to add state:string or ìnteger since you use a value for state, to your model and of course same event
like
event :add_new do
transition new: :analysed
end
as a definition of state machine: is a transition form state to another
in rails c you can call some method for state machine
Issue.first.state_name
Issue.first.state_events
....
if you still have problem try to explain more :)

Resources