Ruby on Rails error when creating record - ruby-on-rails

I am following the Kevin Skoglund tutorial for Ruby on Rails called Ruby on Rails 4 Essential Training. In the section 'Create Record' I am having the following error after simply trying to create a record:
George$ pwd
/Users/George/Sites/simple_cms
George$ rails console
Loading development environment (Rails 4.2.0)
irb(main):001:0> subject = Subject.new
NameError: uninitialized constant Subject
from (irb):1
from /Users/George/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
from /Users/George/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in `start'
from /Users/George/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/George/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/George/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/George/Sites/simple_cms/bin/rails:8:in `<top (required)>'
from /Users/George/.rbenv/versions/2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/George/.rbenv/versions/2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from -e:1:in `<main>'
irb(main):002:0>
As I am still in the early stages of learning I cannot establish what the cause might be although it appears to be gem related based on the errors return.
If anyone has any suggestion and maybe a tip on how I can work out what the error and fix is for myself it would be appreciated as I really want to keep learning.

George$ pwd
/Users/George/Sites/simple_cms
George$ rails console
Loading development environment (Rails 4.2.0)
> rails generate model Subject name:string type:string
> rake db:migrate
> rails console
> sub = Subject.new
Try this.

Related

Ruby on Rails Error Using Rails Methods

I currently have started learning Ruby On Rails. I have Ruby 2.3.1 and Rails 5.1.1 on my Debian Linux. I was following the instructions here : http://railscasts.com/episodes/417-foundation?view=asciicast
However, when I try to use the method rails g scaffold product name price:decimal --skip-stylesheets. I get this error;
"/usr/lib/ruby/vendor_ruby/rails/railtie/configuration.rb:95:in `method_missing': undefined method `load_defaults' for #<Rails::Application::Configuration:0x000000016b9058> (NoMethodError)
from /root/Desktop/RubyOnRails/store/config/application.rb:12:in `<class:Application>'
from /root/Desktop/RubyOnRails/store/config/application.rb:10:in `<module:Store>'
from /root/Desktop/RubyOnRails/store/config/application.rb:9:in `<top (required)>'
from /usr/lib/ruby/vendor_ruby/spring/application.rb:82:in `require'
from /usr/lib/ruby/vendor_ruby/spring/application.rb:82:in `preload'
from /usr/lib/ruby/vendor_ruby/spring/application.rb:143:in `serve'
from /usr/lib/ruby/vendor_ruby/spring/application.rb:131:in `block in run'
from /usr/lib/ruby/vendor_ruby/spring/application.rb:125:in `loop'
from /usr/lib/ruby/vendor_ruby/spring/application.rb:125:in `run'
from /usr/lib/ruby/vendor_ruby/spring/application/boot.rb:18:in `<top (required)>'
from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'"
I get the error, also when I try to generate a controller or model. Because of this error, I can not follow any tutorial. I have tried to reinstall Rails but did not work.How can I solve this? Thank you in advanced.
Use
rails g scaffold product name price:decimal --no-stylesheets
instead --skip
You can do also in config/application.rb something like this.
config.generators do |g|
g.stylesheets false
end
You can see more details here http://guides.rubyonrails.org/configuring.html#configuring-generators
Try following steps:
1. rails new demo
2. cd demo
3. rake db:create
4. rails g scaffold product name:string price:float
5. rake db:migrate
6. rails server
I solved it by reinstalling Ruby and Rails again
Add this line to config/application.rb:
require 'neo4j/railtie'

How to add iframe to sanitized tags in ruby on rails 4.2.6?

I am trying to add iframe to santized tags by using
config.action_view.sanitized_allowed_tags
I tried to find what tags are already allowed by using the console.
uraai#raiuorial:~/workspace/corse (master) $ heroku run rails c
Running rails c on ⬢ fa4... up, run.9396
Loading production environment (Rails 4.2.6)
irb(main):001:0> puts helper.sanitized_allowed_tags.to_a
NoMethodError: undefined method `sanitized_allowed_tags' for #<ActionView::Base:0x007f18ea91ea60>
from /app/vendor/bundle/ruby/2.3.0/gems/metamagic-3.1.7/lib/metamagic/view_helper.rb:30:in `method_missing'
from (irb):1
from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/commands/console.rb:110:in `start'
from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/commands/console.rb:9:in `start'
from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:68:in `console'
from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/commands.rb:17:in `<top (required)>'
from /app/bin/rails:8:in `require'
from /app/bin/rails:8:in `<main>'
irb(main):002:0>
Any idea how to add it without ignoring the other tags? Thanks
Please have a look on the next example:
module Tapp
class Application < Rails::Application
# In config/application.rb
config.action_view.sanitized_allowed_tags = ['strong', 'em', 'a', 'br', 'iframe']
# ...
end
My app called Tapp, I'm pretty sure you will have another name here ;)
Then in the console:
[retgoat#iMac-Roman ~/workspace/tapp]$ rc
Loading development environment (Rails 4.2.6)
[1] pry(main)> Tapp::Application.config.action_view[:sanitized_allowed_tags]
=> ["strong", "em", "a", "br", "iframe"]

Ruby on Rails Michael Hartl Chapter 6.1.3

When loading the Rails console in sandbox as following the tutorial, i can't create a new user object ? I've got this message :
>> User.new
NameError: uninitialized constant User
from (irb):1
from /Users/Genosia/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.1.5/lib/rails/commands/console.rb:90:in `start'
from /Users/Genosia/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.1.5/lib/rails/commands/console.rb:9:in `start'
from /Users/Genosia/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.1.5/lib/rails/commands/commands_tasks.rb:69:in `console'
from /Users/Genosia/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.1.5/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /Users/Genosia/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.1.5/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/Genosia/code/sample_app/bin/rails:8:in `<top (required)>'
from /Users/Genosia/.rbenv/versions/2.1.2/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/Genosia/.rbenv/versions/2.1.2/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from -e:1:in `<main>'
You must have a user class in your app/models folder.
You can use the rails generator for that:
bundle exec rails g model user name:string date_of_birth:date ...
That will create the model under app/models and the migration under db/migrate
To run the migrations run
bundle exec rake db:migrate
bundle exec rails c --sandbox
Then it should work.
Note that opening the console in sandbox mode will rollback all the database changes when you close it.
To have persistent changes run
bundle exec rails c

Loading rails console and defining a variable

Working through Michael Hartl's Ruby on Rails Tutorial and hit a snag on section 2.3.3
"We first invoke the console with rails console at the command line, and then retrieve the first user from the database using User.first (putting the results in the variable first_user):7"
Any help would be much apprecaited
$ rails console
Loading development environment (Rails 3.2.13)
1.9.3-p429 :001 > >> first_user = User.first
SyntaxError: (irb):1: syntax error, unexpected tRSHFT
>> first_user = User.first
^
from /Users/MattPerloe/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
from /Users/MattPerloe/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
from /Users/MattPerloe/.rvm/gems/ruby-1.9.3-p429#rails3tutorial2ndEd/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
You don't need to put >> before your code.
The >> in the tutorial is only to imply that the followed command is being entered in the rails console.
The following will be enough:
rails console
first_user = User.first

Access first record of table in Rails Console Results in NoMethodError

I'm using Rails 3.2.12 and Roby 2.0.0-p0 in RVM etc. In rails console I get the following error after doing u = Users.first:
u = Users.first
Loading production environment (Rails 3.2.12)
2.0.0p0 :001 > u = Users.first
NoMethodError: undefined method `first' for Users:Module
from (irb):1
from /usr/local/rvm/gems/ruby-2.0.0-p0#discourse/gems/railties-3.2.12/lib/rails/commands/console.rb:47:in `start'
from /usr/local/rvm/gems/ruby-2.0.0-p0#discourse/gems/railties-3.2.12/lib/rails/commands/console.rb:8:in `start'
from /usr/local/rvm/gems/ruby-2.0.0-p0#discourse/gems/railties-3.2.12/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
I was able to access the database and view the record via the psql console using the user rails would be using.
If you already have a User model defined, try User.first instead of Users.first.

Resources