nil users after creating 100 of them - ruby-on-rails

I am following Michael Hartl's Ruby on Rails tutorial (9.2)
http://www.railstutorial.org/book/updating_and_deleting_users#cha-updating_showing_and_deleting_users
and I am getting this error.
I am getting this error here
undefined method `each' for nil:NilClass
<h1>All users</h1>
<ul class="users">
<% #users.each do |user| %>
<li>
<%= gravatar_for user, size: 52 %>
<%= link_to user.name, user %>
I created 100 users using gem 'faker', '1.1.2'. I am not sure why there are nil users when I've just created 100 of them.
I also ran these commands
bundle exec rake db:reset
bundle exec rake db:populate
bundle exec rake test:prepare

The problem is NOT that you don't have any users. If you had no users, #users would be an empty array and the .each method would work happily (although there would be nothing to iterate over)
So the problem is that #users is not being initialized correctly.
Look at listing 9.23 on the page you referenced. Do you have this code in your controller?
def index
#users = User.all
end
That's likely what's missing.

Related

rails 2.3 will_paginate 2.3.16 undefined method `paginate' for #<Class:0x7f0ed5f13138>

I've found a lot of links regarding this problem googling. but nevertheless I still get this error after implementing those fixes.
so my gemfile
gem 'will_paginate', '~> 2.3.16'
environment file
config.gem 'will_paginate', :version => '~> 2.3.16'
Controller
def index
#events = Event.paginate({:page => params[:page], :per_page => 10})
end
View:
<% #events.each do |item| %>
...
<% end %>
...
<%= will_paginate #events %>
I'm using rvm, console commands
rvm 1.8.7 do bundle install
...
rvm use 1.8.7 do bundle exec rake gems:install
Could anyone help me with this problem?
try this one
def index
#events = Event.all.paginate({:page => params[:page], :per_page => 10})
end
Note: If u get same error still, then just comment out logic of pagination and insert some records in your database table(model) and then use pagination.
You cannot call paginate on a model. It can be called on collections like hashes, array, ActiveRecord.
It may be just a case of restarting the server after you have installed the gem.

Error After Switching Rails App to Postgres - ActiveSupport::SafeBuffer

I'm new to programming and rails so please forgive any big oversights. I recently deployed a rails app to heroku and switched all environments to postgres for my db. It is up on heroku working just fine with Postgres - ran rake db:drop db:migrate db:create db:seed all to populate my database with a rake task, tested the apps search forms, filters, data queries, etc. and it all worked well.
But I am now getting an error that says "comparison of ActiveSupport::SafeBuffer with nil failed" in the browser after firing up my local server and going to two of my views that have sorts - the browser is pointing me to a line in a view that has
<% #price_array.sort! {|x,y| x <=> y} %>
This is what I see in the server log:
Rendered skis/index.html.erb within layouts/application (212.9ms)
Completed 500 Internal Server Error in 280ms
ActionView::Template::Error (comparison of ActiveSupport::SafeBuffer with nil failed):
51: <% ski.inventories.each do |inventory| %>
52: <% a.push(number_to_currency(inventory.price)) %>
53: <% end %>
54: <% #lowest_price = a.sort { |x,y| x <=> y} %>
55: from <%= #lowest_price.first %>
56: </br>
57: </li>
app/views/skis/index.html.erb:54:in `sort'
app/views/skis/index.html.erb:54:in `block in_app_views_skis_index_html_erb___2303745325449705978_70146899313600'
app/views/skis/index.html.erb:32:in `each'
app/views/skis/index.html.erb:32:in `_app_views_skis_index_html_erb___2303745325449705978_70146899313600'
app/controllers/skis_controller.rb:32:in `index'
This may be related to the issue I am in running into - I cannot seem to drop my postgres database.
This is what I'm seeing in the terminal with postgres:
postgres=# \dbdrop postgres
List of tablespaces
Name | Owner | Location
------+-------+----------
(0 rows)
One other thing I did last night was update my .bash_profile per the instructions found here (http://stackoverflow.com/questions/6770649/repairing-postgresql-after-upgrading-to-osx-10-7-lion). This is what my .bash_profile looks like now:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function
PATH=/usr/local/bin:$PATH
Is there some configuration in postgres that I am missing that is causing this error? Any help or resources would be appreciated. Thanks.
you have nil values in your a array. Try :
a.compact.sort
compact removes all occurences of nil in your array.

How to Install Kaminari Properly?

I'm having a bit problem on how to get Kaminari work. I did the installation procedure in GitHub.
gem "kaminari"
Then run
bundle
I have this snippet for index
#users = User.order("name")
I added this on my view
<%= paginate #users %>
Then I got this error.
undefined method `paginate' for #<#<Class:0x00000102934330>:0x00000102932508>
Did I missed something? I also tried to include the page method
#users = User.order("id").page(1)
But I get this error instead
undefined method `page' for #<ActiveRecord::Relation:0x000001017d0300>
include kaminari and bootstrap-kaminari-views gems in your project Gemfile,
gem "kaminari"
gem "bootstrap-kaminari-views"
Execute bundle install in terminal,
$ bundle install
In products_controller.rb,
#products = Product.order("name")
#products = Kaminari.paginate_array(#products).page(params[:page]).per(5)
In products/index.html.erb,<%= paginate #products, :theme => 'twitter-bootstrap-3' %>
take a look at the railscast for kaminari
its really nice
http://railscasts.com/episodes/254-pagination-with-kaminari
bash
rails g kaminari:views default
products_controller.rb
#products = Product.order("name").page(params[:page]).per(5)
products/index.html.erb
<%= paginate #products %>
app/views/kaminari/_prev_span.html.erb
<span class="prev disabled"><%= raw(t 'views.pagination.previous') %></span>

Trying to call methods from gems in a view, getting "no such file" error

I have just installed a base installation of rails and have edited the main page to view some basic html and a link to create a new blog post via ruby in the corresponding 'erb' file. I am trying to add some additional ruby commands on this same page via <%= %> tags.
<h1>Hello, Rails!</h1> <%= link_to "My Blog", posts_path %>
<p>
<%= require 'rubygems' %>
<%= require 'simplegeo' %>
<%= SimpleGeo::Client.set_credentials('token', 'secret') %>
<%= a = SimpleGeo::Client.get_context(coordinates,coordinates); a %>
</p>
When I load this page , I get the following error: no such file to load -- simplegeo
Can someone point me in the right direction? Many thanks!
In Rails 3, you need to add this gem to your "Gemfile" .. follow this: http://gembundler.com/rails3.html
Remove this completely.. Never do this in your views
<%= require 'rubygems' %>
<%= require 'simplegeo' %>
Once you restart your rails server, if you added "simplegeo" to your gemfile, it'll be auto-required.
Move this to your controller to start
SimpleGeo::Client.set_credentials('token', 'secret')
#simple_geo_client = SimpleGeo::Client.get_context(coordinates,coordinates)
Then in your view, you can access any variable that starts with #
To get started in Rails, check out http://railsforzombies.org/
In rails 3.x, you use Bundler to specify gems. No need to require, especially in views.
Correct way is:
Open Gemfile
Set/List required gems inside using format:
source :rubygems
gem "simplegeo"
gem "some_other_gem"
Run bundle install (or just bundle) command in console.
now restart your server and gems are auto required.
Check out Rails guide on how to start.
Most probably you did not install Simplegeo gem properly, or did not attach it correctly in you IDE.

ruby on rails gravatar_image_tag

I am following the railstutorial.org tutorial. While I am able to see the gravatar on the webpage, my rspec test is failing.
Here is the user_controller_spec.rb test:
describe "GET 'show'" do
before(:each) do
#user = Factory(:user)
end
it "should be successful" do
get :show, :id => #user
response.should be_success
end
end
Here is the relevant show.html.erb file:
<table class="profile" summary="Profile information">
<tr>
<td class="main">
<h1>
<%= gravatar_image_tag(#user.email) %>
<%= #user.name %>
</h1>
</td>
<td class="sidebar round">
<strong>Name</strong> <%= #user.name %><br />
<strong>URL</strong> <%= link_to user_path(#user), #user %>
</td>
</tr>
</table>
I get an error stating "undefined method 'gravatar_image_tag'. I already installed the corresponding gem by executing "gem install gravatar_image_tag", added it to my gemfile, and also executed "bundle install". It is listed as installed when I execute "gem list"
I have been trying to debug this for several hours now. Any help would be appreciated. Thanks in advance. BTW, I'm running 32-bit Windows 7 with GIT Bash, RubyInstaller-1.9.2-p0.
Error message:
Following the same tutorial, I had the same undefined method 'gravatar_image_tag' error, but a different solution from the accepted one above (my gem was in the correct place in the Gemfile, so that wasn't the problem).
Just had to restart the server (Ctrl C then rails server), and then everything was fixed.
Is the gravatar_image_tag gem global to all environments in your gemfile? If you have it in a group :development, :production block, or something similar, it wouldn't be available in test.
And I haven't tried it, but your line:
get :show, :id => #user
seems like it needs to be:
get :show, :id => #user.id
I'm the author of the gem. Can you open an issue on the github page http://github.com/mdeering/gravatar_image_tag/issues with the version of rails and a gem list for me. I will see if I can resolve the issue for you and post an answer back here.
I have some pre-release versions of the gem that I could push out to gemcutter that might resolve the issue already.
Cheers,
Mike D.
If you use Spork as DRB server try to restart it, that helped me! (i'm also following Hartls PDF).
I had the same issue, a simple restart fixed it.
The gravatar gem file has been updated since the release of Michael Hartl's PDF.
gem 'gravatar_image_tag'
Include that in your Gemfile and then restart your rails server using
rails s
That worked for me.

Resources