I'm using Rails 5.0.5 with Devise 4.3.0 for authentication. This app has been running smoothly for months, until I added a 'type'=>'string' attribute to my User model and attempted to create a new user. Submitting the form gives me a 500 internal server error. In this example, the User.type = 'hunter'.
NameError - wrong constant name hunter:
activesupport (5.0.5) lib/active_support/inflector/methods.rb:268:in `const_get'
activesupport (5.0.5) lib/active_support/inflector/methods.rb:268:in `block in constantize'
activesupport (5.0.5) lib/active_support/inflector/methods.rb:266:in `each'
activesupport (5.0.5) lib/active_support/inflector/methods.rb:266:in `inject'
activesupport (5.0.5) lib/active_support/inflector/methods.rb:266:in `constantize'
activesupport (5.0.5) lib/active_support/dependencies.rb:583:in `get'
activesupport (5.0.5) lib/active_support/dependencies.rb:614:in `constantize'
activerecord (5.0.5) lib/active_record/inheritance.rb:177:in `find_sti_class'
activerecord (5.0.5) lib/active_record/inheritance.rb:209:in `subclass_from_attributes'
activerecord (5.0.5) lib/active_record/inheritance.rb:55:in `new'
devise (4.3.0) lib/devise/models/registerable.rb:20:in `new_with_session'
app/models/user.rb:58:in `new_with_session'
user.rb:
def self.new_with_session(params, session)
if session['devise.user_attributes']
new(session['devise.user_attributes']) do |user|
user.attributes = params
user.valid?
end
else
super
end
end
Is Rails think this attribute value is a ClassName?? Can't seem to figure this one out. Any help greatly appreciated.
ActiveRecord uses the type column for Single Table Inheritance (STI) by default and the type value is expected to name a class. Presumably you don't have a hunter class so you get a confusing NameError from deep inside the guts of ActiveRecord.
From the fine manual:
inheritance_column()
Defines the name of the table column which will store the class name on single-table inheritance situations.
The default inheritance column name is type, which means it's a reserved word inside Active Record. To be able to use single-table inheritance with another column name, or to use the column type in your own model for something else, you can set inheritance_column:
self.inheritance_column = 'zoink'
Either rename your type column to something else or tell ActiveRecord to use some other column name for STI:
class User < ApplicationRecord
self.inheritance_column = 'there_is_no_sti_here' # Or whatever you're not using for a column name.
end
Using self.inheritance_column = nil also works.
If you're doing this a lot then you could make it declarative by adding a concern:
module STISuppression
extend ActiveSupport::Concern
included do
self.inheritance_column = nil
end
end
and then say things like:
class SomeModel < ApplicationRecord
include STISuppression
end
Same effect but it makes it clear what you're up to.
I'm unable to logout out of my own page because I get a undefined method Puma error whenever I try. Most of the times it's extract_multipart, but I've also seen before_create.
This is what is shown in the blank page whenever I click logout
Puma caught this error: undefined method `extract_multipart' for Rack::Multipart:Module (NoMethodError)
/var/lib/gems/2.3.0/gems/rack-2.0.1/lib/rack/request.rb:472:in `parse_multipart'
/var/lib/gems/2.3.0/gems/rack-2.0.1/lib/rack/request.rb:335:in `POST'
/var/lib/gems/2.3.0/gems/rack-2.0.1/lib/rack/method_override.rb:39:in `method_override_param'
/var/lib/gems/2.3.0/gems/rack-2.0.1/lib/rack/method_override.rb:27:in `method_override'
/var/lib/gems/2.3.0/gems/rack-2.0.1/lib/rack/method_override.rb:15:in `call'
/var/lib/gems/2.3.0/gems/rack-2.0.1/lib/rack/runtime.rb:22:in `call'
/var/lib/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
/var/lib/gems/2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/executor.rb:12:in `call'
/var/lib/gems/2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/static.rb:136:in `call'
/var/lib/gems/2.3.0/gems/rack-2.0.1/lib/rack/sendfile.rb:111:in `call'
/var/lib/gems/2.3.0/gems/railties-5.0.1/lib/rails/engine.rb:522:in `call'
/var/lib/gems/2.3.0/gems/puma-3.6.2/lib/puma/configuration.rb:225:in `call'
/var/lib/gems/2.3.0/gems/puma-3.6.2/lib/puma/server.rb:578:in `handle_request'
/var/lib/gems/2.3.0/gems/puma-3.6.2/lib/puma/server.rb:415:in `process_client'
/var/lib/gems/2.3.0/gems/puma-3.6.2/lib/puma/server.rb:275:in `block in run'
/var/lib/gems/2.3.0/gems/puma-3.6.2/lib/puma/thread_pool.rb:116:in `block in spawn_thread'
The way I'm managing log-outs is with
<%= link_to(logout_path, method: 'delete', class: 'dropdown-item') do %>
<!-- ... -->
<% end %>
Where the route is defined as delete 'logout' => 'sessions#destroy', and the controller / action is
def destroy
session[:id] = nil
redirect_to '/login'
end
The other error I have only caught once or twice and all I have is a screenshot
Puma: undefined before_create
Any ideas on what might be causing this? Thanks beforehand :)
Edit
As per request, here's the Enumerable concern, which I'm not actually using because I'm also having issues with that (GH issue submitted on auto-inc repository)
require 'autoinc'
module Enumerable
extend ActiveSupport::Concern
included do
include Mongoid::Autoinc
include Mongoid::Document
field :n, as: :number, type: Integer
increments :number
end
end
And the only before_create I'm using is within a completely unrelated module...
Edit 2
Tried updating puma to 3.7.0, didn't fix it. Although it didn't break it further...
Answer obtained from
Delete link sends "Get" instead of "Delete" in Rails 3 view
I needed to add the <%= csrf_meta_tag %> to my head.
I'd like to add a vendor? function to Spree::User.
So I created app/models/spree/user.rb
module Spree
class User < Spree::Base
include Spree::UserMethods
def vendor?
self.role_users.any? { |ru| ru.role.name == 'vendor' }
end
end
end
This works if I start rails console and test:
u = Spree::User.last; u.vendor? # => false
But when I start rails server and visit a page I get this error:
NoMethodError in Spree::Admin::OrdersController#index
undefined method `serialize_from_session' for #
Stack trace
activerecord (4.2.5) lib/active_record/dynamic_matchers.rb:26:in `method_missing'
devise (3.5.4) lib/devise.rb:465:in `block (2 levels) in configure_warden!'
warden (1.2.4) lib/warden/session_serializer.rb:34:in `fetch'
warden (1.2.4) lib/warden/proxy.rb:212:in `user'
warden (1.2.4) lib/warden/proxy.rb:322:in `_perform_authentication'
warden (1.2.4) lib/warden/proxy.rb:104:in `authenticate'
devise (3.5.4) lib/devise/controllers/helpers.rb:124:in `current_spree_user'
solidus_auth_devise (1.3.0) lib/spree/authentication_helpers.rb:11:in `spree_current_user'
solidus/core/lib/spree/core/controller_helpers/auth.rb:67:in `try_spree_current_user'
solidus/core/lib/spree/core/controller_helpers/auth.rb:31:in `current_ability'
cancancan (1.13.1) lib/cancan/controller_additions.rb:338:in `authorize!'
solidus/backend/app/controllers/spree/admin/base_controller.rb:22:in `authorize_admin'
Any idea what I should do to fix this error? According to the stack trace it looks like I'm missing a method somewhere.
Cheers,
Martin
The answer is to open the Spree::User class and add the methods you're missing:
E.g:
config/initializers/spree_user.rb
Spree::User.class_eval do
def vendor?
self.role_users.any? { |ru| ru.role.name == 'vendor' }
end
end
I am currently working on the Ruby on Rails tutorial by Michael Hartl. I am trying to add a page for each user in my database by creating an HTML with embedded ruby page in the views directory. The code for show.html.erb is below:
<%= #user.name %>, <%= #user.email %>
When I add the user to the user_controller.rb file, it looks like this:
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
end
def new
end
end
When I run the rails server and click on open up the users/1 URL, I get a NameError complaining about an uninitialized constant. The error and trace is below:
NameError in UsersController#show
uninitialized constant UsersController::User
Rails.root: /usr/sample_app
Application Trace | Framework Trace | Full Trace
app/controllers/users_controller.rb:3:in `show'
actionpack (3.2.12) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.12) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.12) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.12) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.12) lib/active_support/callbacks.rb:414:in
.
.
.
.
Please let me know how to go about this because I cannot pass my spec tests with this error. If anyone has any suggestions or insight I would greatly appreciate them.Thank you.
Controller file name should be users_controller.rb
As you mentioned in comment you don't have any User model defined. To create model run rails g model user name:string email:string. This will create User model with attributes name and email.
Try Rails scaffolding and see what files it creates(in controller, model and views ignore other files for now) and see the contents of those files. To use scaffold for creating User is as below:
rails g scaffold user name:string email:string
rake db:migrate
It seems like some people here had this problem but I couldn't find any solution in another topic.
I am doing Chapter 3 of the Ruby on Rails-Tutorial, working on the static pages. When I want to open them on the localhost it gives me a "Routing Error" in the Browser.
My Ruby is currently on version 1.9.3.
My Rails is currently on version 3.2.
I have tried:
restarting the server
saving all the files again
checking any issues in the static_pages_controller.rb
checking any issues in the routes.rb
checking any issues in the static_oages_spec.rb
Also there are no bugs in the HTML code of the single static page. And I can't find any more help in the tutorial, neither in other questions here on StackOverflow.
Edit:
This is the actual error message from the browser:
Routing Error
No route matches [GET] "/static_pages/home" Try running
rake routes for more information on available routes.
if I go to http://localhost:3000/static_pages/home, to one of three static pages I have.
This is routes.rb:
SampleApp::Application.routes.draw do
get "static_pages/home"
get "static_pages/help"
get "static_pages/about"
end
Also, I tried "rake routes" in the terminal, too. This is the result:
home_static_pages GET /static_pages/home(.:format) static_pages#home
help_static_pages GET /static_pages/help(.:format) static_pages#help
about_static_pages GET /static_pages/about(.:format) static_pages#about
static_pages POST /static_pages(.:format) static_pages#create
new_static_pages GET /static_pages/new(.:format) static_pages#new
edit_static_pages GET /static_pages/edit(.:format) static_pages#edit
GET /static_pages(.:format) static_pages#show
PUT /static_pages(.:format) static_pages#update
DELETE /static_pages(.:format) static_pages#destroy
And this is the error message the server is giving me:
Started GET "/static_pages/home.html" for 127.0.0.1 at 2012-04-03 13:23:54 +0200
ActionController::RoutingError (No route matches [GET] "/static_pages/home.html"):
actionpack (3.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.3) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.3) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.3) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.1) lib/rack/lock.rb:15:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.3) lib/rails/engine.rb:479:in `call'
railties (3.2.3) lib/rails/application.rb:220:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.3) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
/Network/Servers/pluto.kayoom.lan/Users/benediktkrebs/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/Network/Servers/pluto.kayoom.lan/Users/benediktkrebs/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/Network/Servers/pluto.kayoom.lan/Users/benediktkrebs/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
Try this :
match "/static_pages/home" => "static_pages#home", :via => :get
match "/static_pages/help" => "static_pages#help", :via => :get
match "/static_pages/about" => "static_pages#about", :via => :get
Add into routes , restart the server and refresh browser .
If you are using Spork you must re-run Spork so your tests will consider the changes in config files like routes.rb, that are pre-loaded with Spork, so are not automatically updated.
Stop Spork (Ctrl + C)
Run Spork again (bundle exec spork)
Source: this is the source of this information
"after changing a file included in the prefork loading (such as routes.rb), you will have to restart the Spork server to load the new Rails environment.
If your tests are failing when you think they should be passing, quit the Spork server with Control-C and restart it
your routes.rb file has problems, use either RESTful style:
resource :static_pages do
collection do
get :home
get :help
get :about
end
end
or the non RESTful style:
match "static_pages/home", :controller => "static_pages", :action => "home"
match "static_pages/help", :controller => "static_pages", :action => "help"
match "static_pages/about", :controller => "static_pages", :action => "about"
for more details please refer to official guide: http://guides.rubyonrails.org/routing.html
I had a few problems working through the Rails Tutorial, and it helped to be able to consult the author's GitHub repo: https://github.com/railstutorial
Find the file you're working on and compare it line by line. Or just cut and paste the full file and see if it will run, then track down your error.
If you check the routes.rb in config you'll probably find that the there is a 'e' missing from /home. Add that and you are golden. Or green. Or whatever :)
check your http:// address, specifically if you are on localhost:3000/path or localhost:3000/path1/path2, etc
Once you change the format of your mapping from get to match you will not need static_pages anymore, just go straight to localhost:3000/pagename e.g localhost:3000/about
From Ruby 2 to Ruby 3 there are some differences, but still the documentation for 3 is not easy to be found. There should be a tutorial only for that: practical differences between rails 2 and 3.
The guide provided by downloading Ruby on rails is "The book of ruby" but it's not good anymore. It should at least contain an advice at the beginning of chapter 19.
In
config/routes.rb
uncomment
root :to => 'welcome#index'