Hello i'm a beginner following the Lynda ruby on rails tutorial.
Here is the code I have to run subject = Subject.new. but everytime i type that into the rails console, I get this error.
NameError: uninitialized constant Subject
from (irb):1
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/co
mmands/console.rb:90:in `start'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/co
mmands/console.rb:9:in `start'
from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/co
mmands.rb:64:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
new is a method of Class. With your code
subject = Subject.new
you want to create an instance of the class Subject, but you it seems that you haven't defined a class Subject.
On irb you can do it this way:
class Subject
end
and now with
subject = Subject.new
you can create an instance of Subject, what you can test with
subject.class
=> #<Subject:0x007fca538325c8>
But this all makes not much sense if you haven't defined any methods for the class Subject. And this is all is essential ruby or essential OOP, so try to get some basic stuff about Ruby. I suggest Rubymonk or something else.
Related
I am currently following a recipe box tutorial on youtube.
When I open the rails console and type
#recipe = current_user.recipes.build
I got the following error:
NameError: undefined local variable or method `current_user' for main:Object
from (irb):3
from /Users/matthiascordes/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0.1/lib/rails/commands/console.rb:65:in `start'
from /Users/matthiascordes/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0.1/lib/rails/commands/console_helper.rb:9:in `start'
from /Users/matthiascordes/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:78:in `console'
from /Users/matthiascordes/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /Users/matthiascordes/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0.1/lib/rails/commands.rb:18:in `<top (required)>'
from bin/rails:9:in `require'
from bin/rails:9:in `<main>'
Does anybody has an idea what is wrong and how I can solve this problem?
I am using the devise gem for my user model.
you can not access current_user inside your rails console,so try
user = User.find(your_user_id)
user.recipes.build
current_user is a variable provided by one of the before_filters of devise, therefore, outside a controller you don't have access to it, if you still want to be in that context, you can use gem byebug and drop a byebug inside your code, or a debugger and the execution will stop and you will be in the scope you need, to test all you are required, otherwise, plain in rails c, you will need to fetch the user the old way, with a query
User.find(user_id)
You cannot access current_user in rails console .
If you want the user variable, then search it like
user = User.find(some_id)
& then use it like user.recipes.build
you can also create the user from rails console like user = User.create(user parameters) & then use it building receipies
(Rails 4.2.4) Hello, beginner here. I am working on a project which does not need a DB or activeRecord. Therefore, when making my Rails project I appended the -O (to disable Active Record and database) (rails new MyApp -O)
I read that to do a model not backed by a database you can just create a file in
app/models/site.rb.
No need to do:
rails generate model Site
So I added my model, which looks something like this:
class Site
attr_reader :name
attr_reader :out_average
attr_reader :in_average
attr_reader :change
def initialize(name, in_average, out_average)
#name = name
#out_average = out_average
#in_average = in_average
#change = find_increase
end
def find_increase()
if #in_average && #out_average != 0
#change = ((#in_average - #out_average)/#out_average)*100
else
#change = 0
end
return #change
end
end
So, I then started up console "rails c" and when I try to invoke a new Site object, I get an error:
irb(main):001:0> Site.new
NameError: uninitialized constant Site
from (irb):1
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands/console.rb:110:in `start'
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands/console.rb:9:in `start'
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:68:in `console'
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands.rb:17:in `<top (required)>'
from /home/ms-87/Documents/projects/rails_projects/site_seasonality/bin/rails:8:in `<top (required)>'
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from -e:1:in `<main>'
I made sure I started the console from the root of my app. I also made sure to use the proper naming convention (site.rb is the filename in app/model/, "Site" is the name of my class inside the file). Can anyone help me as to why this isn't working? Is my thinking here wrong? Thanks.
My first error was that my filenames were capitalized "Site.rb", I had actually fixed this before I posted. But after I fixed it, I accidentally started using "irb" instead of "rails c". DOH! Sorry for the post.
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.
Probably something wrong with my setup:
irb(main):001:0> truncate("Once upon a time in a world far far away", :length => 17)
NoMethodError: undefined method `truncate' for main:Object
from (irb):1
from /usr/lib64/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start'
from /usr/lib64/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start'
from /usr/lib64/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
Looks like I cannot use any text helpers (both in irb and rails console).
What should I check?
The Rails Console exposes the helper methods through the helper variable. Therefore, please, use this instead:
helper.truncate("Once upon a time in a world far far away", :length => 17)
for more, please read this article on 37signals.com
type following line into your rails console
include ActionView::Helpers
now your helpers are accessible during the entire rails console session and you can continue likeā¦
truncate("Once upon a time in a world far far away", :length => 17)
In my model for tutoring sessions, I have code that triggers reminder texts at different times. Everything worked fine, until I tried some refactoring, and now I am having issues.
def send_reminder_text(texts_batch)
texts_batch.each do |text|
page_number = Refugee.find(text.refugee_id)[:last_page]
body_of_text = text[:begin_time].in_time_zone.strftime("Burma Reminder: upcoming session at %I:%M%p beginning on page #{page_number}.
Please email jek2141#columbia.edu to reschedule or cancel the session.")
text.begin_text(body_of_text)
end
end
def self.deliver_pm_reminder_text
texts_batch = TutoringSession.batch_for_pm_reminder_text
send_reminder_text(texts_batch)
end
def self.deliver_just_before_reminder_text
texts_batch = TutoringSession.batch_for_just_before_reminder_text
send_reminder_text(texts_batch)
end
When I call the deliver_just_before_reminder_text function, I get the following error message:
irb(main):006:0> TutoringSession.send_reminder_text
NoMethodError: undefined method `send_reminder_text' for #<Class:0x00000003cfe5d8>
from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record /base.rb:1088:in `method_missing'
from (irb):6
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.1.1/lib/rails/commands/console.rb:45:in `start'
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.1.1/lib/rails/commands/console.rb:8:in `start'
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.1.1/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
This is in spite of the fact that the send_reminder_text is clearly defined above.
Your method declaration specifies that the method is on objects of the TutoringSession class:
def send_reminder_text(texts_batch)
But you're trying to call it as if it were a method on the class itself:
irb(main):006:0> TutoringSession.send_reminder_text
Try changing your definition to:
def self.send_reminder_text(texts_batch)