Undefined method 'on' for ActionModel - ruby-on-rails-3

I'm getting the following error:
NoMethodError in Users#new
Showing .../app/views/users/form/_new.haml where line #7 raised:
undefined method `on' for #<ActiveModel::Errors:0x007fb599ec6610>
The code in line 7 is:
7: = errors_for user, :first_name
And the application_helper.rb:
def errors_for(model, field)
error = case errors = model.errors.on(field)
...
end
'on' is a default method in ActiveRecord. Why doesn't this work?

If you're using Rails 3, then the problem is that there's no "on" method for the Errors class anymore. I think you're supposed to use "get" now. So:
error = case errors = model.errors.get(field)
Or...
error = case errors = model.errors[field]

I checked my user and u.errors is an ActiveRecord::Errors, while I see you have an ActiveModel::Error, I would work on that.
Then I don't understand case errors = statement in your helper, I'm curious to know how you implemented that part...

Related

Getting an invalid token on an interpolated string sent from python/jinga2 backend

I'm sending a variable called apiID from a tornado/jinja2 python file to my vuejs template like this:
class SmartAPIUIHandler(BaseHandler):
def get(self, yourApiID):
doc_file = "smartapi-ui.html"
dashboard_template = templateEnv.get_template(doc_file)
dashboard_output = dashboard_template.render(apiID = yourApiID )
self.write(dashboard_output)
then in vuejs I'm interpolating the variable with no problem except it gives me an error
it says: Uncaught SyntaxError: Invalid or unexpected token
I checked on the python handler file and apipID is a string, so I don't see the problem. I'm quite new to python so maybe the answer is more obvious to one of you. I appreciate the help!!
Because of dashboard_output = dashboard_template.render(apiID = yourApiID ), you must have, in your template, something around the code:
this.apiID = {{ apiID }};
Due to the value being not a number but a string, add the 's:
this.apiID = '{{ apiID }}';

RSpec: Cant Verify there are more than 5 Btns on Search Result Page

I try to do an Selenium-Acceptancetest 'Search Result List' with RSpec spec
And my Issue is
It should verify the count of a Button "Detail"
I would be happy to get help for start thinking as a coder; i am a manual tester stil.
My Problem now is:
Method Error
Failure/Error: expect(#driver.find_element(:xpath, "//a[contains(text(),'Details')]").to be > 4)
NoMethodError:
undefined method `to' for #<Selenium::WebDriver::Element:0x00000003fd4938>
Sources:
On my way to resolve the issue I tried to modify this and I found this
But its not working
thanksfully remaining!
my Test File:
# coding: utf-8
puts "this is #{File.basename(__FILE__)}"
extend RSpec::Expectations
extend RSpec::Matchers
describe 'SEL' do
before(:each) do
#driver = loadDriver()
end
after(:each) do
#driver.quit
end
it 'test_Page (SEL)' do
#get the page
...
#do input keyword 'Restaurant'
...
#click submit
...
#(Works!) temp Validation1: Is there a "Btn Details" in SearchResultList?
expect(#driver.find_element(:xpath, "//a[contains(text(),'Details')]").displayed?)
#(Works not!) Validation: Are there more than "5 Btns Detail"
expect(#driver.find_element(:xpath, "//a[contains(text(),'Details')]").to be > 5)
end
end
Update after first answer:
Given I use
expect(#driver.find_elements(:xpath, "//a[contains(text(),'Details')]")).to be > 0
it hits that error:
Failure/Error: expect(#driver.find_elements(:xpath, "//a[contains(text(),'Details')]")).to be > 0
expected: > 0
got: [#<Selenium::WebDriver::Element:0xa6219008 id="a2a2c83a-e52d-4464-81ec-4fce07ccc0b6">, #<Selenium::We...64378279">, #<Selenium::WebDriver::Element:0x..f90cc8d0e id="de990e21-eecf-48db-873b-76515dba7c3e">]
Given the error message:
undefined method `to' for #<Selenium::WebDriver::Element:0x00000003fd4938>
I suspect your issue is a missing bracket. Normal Rspec syntax is
expect(something).to eq(some value).
I would imagine you are missing the closing parenthesis
expect(#driver.find_element(:xpath, "//a[contains(text(),'Details')]").to be > 5)
and this should be something more like:
expect(#driver.find_element(:xpath, "//a[contains(text(),'Details')]")).to be > 5
Although I might guess you need to call a size function on the latter part as well as maybe use find elements. Maybe this will also be useful for you.

How to handle exceptions caused by nil:NilClass

I have a model called quiz, which has many questions models. I want to add some kind of esception handling so that when the user types in a wrong quiz_id in the URL, an error page would be rendered.
I wrote some helper methods in my QuestionsController to handle the exceptions:
private
def render_error(message)
#error_message = message
render 'error'
end
def active_quizzes_safe
active_quizzes = Quiz.active_quizzes(current_user.id)
render_error('Sorry! The request is invalid! Please log in again!') if active_quizzes.nil?
active_quizzes
end
def active_quiz_safe(quiz_id)
active_quiz = active_quizzes_safe.where(id: quiz_id).first
render_error('The quiz does not exist or you are not allowed to take this quiz!') if active_quiz.blank?
active_quiz
end
And here is the action in QuestionsController which has problems:
def show_quiz
if current_user
#quiz = active_quiz_safe(params[:quiz_id])
#questions = #quiz.questions
end
end
So if the :quiz_id in the URL localhost:3000/MY_URL/:quiz_id is not correct(that is, a record cannot be found), an error page should be rendered by the render_error mothod. However, when I tired a wrong :quiz_id, I got undefined method 'questions' for nil:NilClass. I guess this is because of the #questions = #quiz.questions in show_quiz method.
However, is the execution supposed to halt after the render_error action, which is before #questions = #quiz.questions? Why #questions = #quiz.questions is executed anyway?
In addtion, are there any standard ways to handle nil:NilClass errors like this?
Thank you!!
Look in your public/404.html, public/422.html and public/500.html files. Rails will automatically redirects if error occurs anyway. So I think you don't need to manually handle exceptions, except you have specific case. To test and view this error pages run application in production bundle exec rails s RAILS_ENV=production.
Calling render method does not halt the action. So you should carefully design your action to ensure that you return immediately after rendering. Like this:
def show_quiz
if current_user
active_quizzes = Quiz.active_quizzes(current_user.id)
if active_quizzes.nil?
render_error('Sorry! The request is invalid! Please log in again!')
else
#quiz = active_quizzes_safe.where(id: quiz_id).first
if #quiz.blank?
render_error('The quiz does not exist or you are not allowed to take this quiz!')
else
#questions = #quiz.questions
end
end
end
end
But in this case, I think it's better to use some exception control, like this:
def show_quiz
if current_user
active_quizzes = Quiz.active_quizzes(current_user.id)
#quiz = active_quizzes_safe.find(quiz_id)
#questions = #quiz.questions
end
rescue ActiveRecord::RecordNotFound
render_error 'The quiz does not exist or you are not allowed to take this quiz!'
end

Rails 3: Pass options to model method

This is my Model (Subscription) Method:
def activation_codes(options = {})
if options[:first]
self.group.group_codes.first
else
self.group.group_codes
end
end
I am trying to call to this method in this fashion:
sub = Subscription.where(:subscription_limit => -1).first
sub.activation_codes {:first}
For some reason the elseis being evaluated.
You need to pass Hash to method activation_codes in order to make it work as expected, like:
sub.activation_codes({:first => 'some value'})
but you're currently passing Symbol instead.

RAILS EXCEPTION: undefined method `keys' for nil:NilClass

I can't figure out why in the following line of code
res = https.get(url, nil)
The application stops and give me the following exception:
undefined method `keys' for nil:NilClass
The strange things is that this error happens only in my development enviroment, when I put the application online (heroku), everyhthing work like a charm.
That above line of code, use this inclusion:
require 'net/https'
Is anybody able to explain me why ?
Tnx
If you don't want to pass any headers just use:
res = https.get(url, {})
in the documentation for Net::HTTP this method definition is
def get(path, initheader = {}, dest = nil, &block) # :yield: +body_segment+
res = nil
if HAVE_ZLIB
unless initheader.keys.any?{|k| k.downcase == "accept-encoding"}
initheader["accept-encoding"] = "gzip;q=1.0,deflate;q=0.6,identity;q=0.3"
#compression = true
....
in your code initheader is nil so error happens on line four initheader.keys It may work fine on heroku because there is if HAVE_ZLIB which can be faulse so code is skipped.