undefined method `[]' for nil:NilClass - ruby-on-rails-3

I'm following the Michael Hartl tutorial and I found a problem and don't know how solved it... my problem is around here whitout ajax.
when I try to do follow to other user I got this:
NoMethodError (undefined method `[]' for nil:NilClass):
app/controllers/follows_controller.rb:4:in `create'
here is my "follows" controller eq to relationships
before_filter :authenticate_user!
def create
#user = User.find(params[:follows][:followed_id])
current_user.follow!(#user)
redirect_to #user
end
def destroy
#user = Follow.find(params[:id]).followed
current_user.unfollow!(#user)
redirect_to #user
end
the rest (models,rspec) I have the same
if you need more code tell me, thanks

Chances are that params[:follows] is nil. Check what parameters you're submitting to the create action.

Related

Before filter in action mailer Rails 3

What I need:
Something similar to before_filter in ActionMailer in Rails 3.
Problem:
I am working on Rails 3 and want to have a before_filter in ActionMailer. Checked the actionmailer api and learned about before_action and after_action callbacks. When implemented it gives the error:
NoMethodError: undefined method `before_action' for Notifier:Class
Later learned that there is no before action call for Rails 3 from this post
Isnt there any hook or gem so that we can have something similar like before_filter in Rails 3.
Please help. Many Thanks!!
It can be achieved by including AbstractController::Callbacks. This mimics the change to Rails 4 which apart from comments and tests, just included Callbacks.
class MyMailer < ActionMailer::Base
include AbstractController::Callbacks
after_filter :check_email
def some_mail_action(user)
#user = user
...
end
private
def check_email
if #user.email.nil?
message.perform_deliveries = false
end
true
end
end
Reference - How to add a before_filter in UserMailer which checks if it is OK to mail a user?

How do I stub part of my session to simulate an authorized session?

I have a before filter on my Products Controller:
before_filter :authorize, only: [:create, :edit, :update]
my authorize method is defined in my application_controller as:
def authorize
redirect_to login_url, alert: "Not authorized" if current_user.nil?
end
and current_user is defined as:
def current_user
#current_user ||= User.find(session[:user_id]) if session[:user_id]
end
In my rspec, I am trying:
before(:each) do
session.stub!(:user_id).and_return("admin#email.com")
end
But I am still getting an error as follows:
ProductsController PUT update with valid params redirects to the product
Failure/Error: response.should redirect_to(product)
Expected response to be a redirect to <http://test.host/products/1> but was a redirect to <http://test.host/login>
. . . which means that my test is not logged in at the time of the request.
What am I missing here?
Is there a better way to approach this situation?
Considering you are testing the controller, and trying to keep it focussed on the controller you could stub the current_user method with a real user or a mock.
before(:each) do
ApplicationController.any_instance.stub(:current_user).and_return(#user = mock('user'))
end
With that you will have access to the user mock to apply further expectations and stubs if needed.
If the mock gets in the way, change it out for a real User object.

Why does this method not prevent NoMethodError?

I'm using the will_paginate gem, and trying to implement DataTables, per this RailsCast, and discovered that it is a good idea to supply the attribute total_entries, because will_paginate apparently does not do this very well.
So I added this method to my class like so:
class Genotype < ActiveRecord::Base
attr_accessible :allele1, :allele2, :run_date
belongs_to :gmarkers
belongs_to :gsamples
def total_entries
#t = Genotype.count;
return #t
end
end
However, I still get this error when the view tries to load (this is a screenshot of the webserver responses):
(7.6ms) SELECT COUNT(*) FROM "genotypes"
Completed 500 Internal Server Error in 96ms
NoMethodError (undefined method `total_entries' for nil:NilClass):
app/datatables/genotypes_datatable.rb:13:in `as_json'
app/controllers/genotypes_controller.rb:7:in `block (2 levels) in index'
app/controllers/genotypes_controller.rb:5:in `index'
Obviously I must be doing something wrong in defining my method; but what? I am too inexperienced in Rails to stumble my way out of this...
Thanks in advance,
Rick

Rails3 + Rspec2: undefined method `authenticate!' for nil:NilClass

All my tests fail when I add before_filter authenticate_user! to my controllers. How do I get by this?
Thanks
You have to login a user in your tests. I don't know the authentication method you use, but i will make a wild guess. If it's devise, create a spec/support/controller_macros.rb and insert :
module ControllerMacros
def login_user
before(:each) do
#request.env["devise.mapping"] = :user
#user = Factory.create(:user)
sign_in #user
end
end
end
I use factory girl to create a factory, but you can do it however you like. Then, in your tests add it like :
describe AlliancesController do
login_user
describe "GET 'show' without an id" do
......
put include Devise::TestHelpers to your spec
http://pupeno.com/2010/09/26/undefined-method-authenticate/

Rails 3: NoMethodError in ReviewsController#create

I have a relationship user (has_many :reviews) and review ( belongs_to :user) and I'm getting a NoMethodError when I try to execute create in reviews_controller.
def create
#review = #user.reviews.create(:service_id => 10)
end
The same query works fine in the console but it gives that error when I do it through the controller.
What am I missing here?
Thank you.
Print out #user details to log to verify current_user is populating the value correctly.