jeudi 7 mai 2015

Rails ActionView::Template::Error: undefined method for Nill class

I know that this is quite a common problem but none of the other responses has been able to help me.

I am currently trying to create tests for my website but I always get the error

ActionView::Template::Error: undefined method

for quite a lot of my methods which usually result in a Nill class. The website uses devise for logging in.

Here is a test that I'm trying to run. I made sure my fixtures are loaded into the test database

    require 'test_helper'

class PagesControllerTest < ActionController::TestCase
  include Devise::TestHelpers
  include Warden::Test::Helpers
  Warden.test_mode!

  def setup
    sign_in User.first
  end

  def teardown
    Warden.test_reset!
  end

  test "should get index" do
    get :index
    assert_response :success
  end


  test "should get voting" do
    get :voting
    assert_response :success
  end
end

And these are the error messages when trying to run the test

Minitest::UnexpectedError: ActionView::Template::Error: undefined method `strip' for nil:NilClass
    config/initializers/addChallenges.rb:22:in `findChallenges'
    app/views/pages/index.html.erb:11:in `_app_views_pages_index_html_erb__190802989_69818040'
    test/controllers/pages_controller_test.rb:18:in `block in <class:PagesControllerTest>'
config/initializers/addChallenges.rb:22:in `findChallenges'
app/views/pages/index.html.erb:11:in `_app_views_pages_index_html_erb__190802989_69818040'
test/controllers/pages_controller_test.rb:18:in `block in <class:PagesControllerTest>'

Minitest::UnexpectedError: ActionView::Template::Error: undefined method `strip' for nil:NilClass
    config/initializers/addIdeas.rb:25:in `findIdeas'
    app/views/pages/voting.html.erb:6:in `_app_views_pages_voting_html_erb___557022735_70668940'
    test/controllers/pages_controller_test.rb:24:in `block in <class:PagesControllerTest>'
config/initializers/addIdeas.rb:25:in `findIdeas'
app/views/pages/voting.html.erb:6:in `_app_views_pages_voting_html_erb___557022735_70668940'
test/controllers/pages_controller_test.rb:24:in `block in <class:PagesControllerTest>'
Finished in 0.31402s
2 tests, 0 assertions, 0 failures, 2 errors, 0 skips

Process finished with exit code 0

When tracking the error in this case, this line is shown as problematic resp = http.get(url.path, headers) This is my full addIdeas code but the addChallenges one is quite similar.

class AddIdeas
  #a method that will find all the challenge ideas for a user and then store them in our databse
  def self.findIdeas(email,challengeId)

    require "net/http"
    require "uri"
    require 'json'
    require 'active_record'

    p= People.find_by_email(email)

    uri_string = 'http://ift.tt/1AItKSd'
    uri_string << challengeId.to_s

    #make the http request with the headers
    url = URI.parse(uri_string)
    http = Net::HTTP.new(url.host, url.port)
    headers = {
        'Authorization' => p.basic,
        'UserId' => p.userId,
        'AuthorizationToken' => p.auth_tok
    }

    #retrieve a get response
    resp = http.get(url.path, headers)
    #if response is okay parse the challenge ids and add them to the person table for that user
    if resp.code.to_s == '200'
        if resp.body.to_s != 'null'
          puts parsed = JSON.parse(resp.body)
          ids = ""
          parsed.each do |element|
            addIdeas(element, challengeId)
            ids << "#{element['IdeaId']},"
          end
          c = Challenges.find_by_challengeId(challengeId)
          c.ideaIds = ids
          c.save
        end
    end
  end

  def self.addIdeas(element, challengeId)
    i = Ideas.find_by_ideaId(element['IdeaId'])

    if i == nil
      i = Ideas.create(:ideaId => element['IdeaId'], :title => element['Title'], :description => element['Description'], :challengeIds => challengeId, :score=>1000, :faceOff => 0, :wins =>0)
    end
    if i != nil
      i.ideaId = (element['IdeaId'])
      i.title = (element['Title'])
      i.description = (element['Description'])
      i.challengeIds = challengeId
      i.save
    end
  end


  def self.findAllIdeas(email)
    p = People.find_by_email(email)
    ids = p.challenges
    splitted = ids.split(",")
    counter = splitted.length
    i =0
    while i < counter.to_i do
      findIdeas(email, splitted[i])
      i += 1
    end
  end
end

I have tried modifying the methods with try() and it seems to sometimes resolve the issue but after that the try() causes problems with the website itself. Sometimes the error message would redirect me to the html view file itself where methods are used.

Aucun commentaire:

Enregistrer un commentaire