Error in running RSpec. 2.13.1 - ruby-on-rails

I have written a spec file for update function of one controller. I am using ruby-1.9.3 p194, Rails 3.2.13 and rspec 2.13.1 and data mapper version ~> 1.2.0'. I am calling update function in my spec as follows:
put :update, :id=>#xyz.id, :xyz=> {key1:val1, key2:val2}
In update function of my xyz controller I have accessed id as follows:
def update
params[:xyz][:id] = params[:id] #This line is working fine and params[:id] is not nil
params = params[:xyz]
id = params[:id]
end
Problem is that while I am running the same file without any change twice at a time, it is giving two different errors. Once it is giving the following error:
/home/joy/.rvm/gems/ruby-1.9.3-p194#packageName/gems/dm-core-1.2.0/lib/dm-
core/collection.rb:1302: in resource undefined method 'saved?' for nil:NilClass
(NoMethodError)
....
Next time, while running, it is giving the following error:
NoMethodError:
undefined_method 'id' for "57":string"
# ./app/controllers/xyz_controller.rb : in line number 3 of update where I am setting the id is giving error.
Is this the case the ruby version or the rspec or data-mapper version I am using is unstable or there is any other problem that I could not figure out. Please help.

Related

Rails 3 to Rails 4 upgrade undefined method to_input_field_tag

I'm in the process of upgrading a Rails 3 application to Rails 4 and I get the error message undefined method to_input_field_tag for class ActionView::Helpers::InstanceTag NameError when trying to start the Rails server.
Looking at API Dock http://apidock.com/rails/ActionView/Helpers/InstanceTag/to_input_field_tag it shows this method as deprecated or moved.
Has this been moved or what is an equivalent replacement for this in Rails 4?
The method has been deprecated and it appears that
ActionView::Helpers::InstanceTag
has been refactored into:
ActionView::Helpers::ActiveModelInstanceTag
Comparing the source code between Rails 3 and 4 it appears that the way input fields are rendered has changed:
# ~/rails-3-2-stable/actionpack/lib/action_view/helpers/form_helper.rb
def text_field(object_name, method, options = {})
InstanceTag.new(object_name, method, self, options.delete(:object)).to_input_field_tag("text", options)
end
Changed to:
# ~/rails-4-2-stable/actionview/lib/action_view/helpers/form_helper.rb
def text_field(object_name, method, options = {})
Tags::TextField.new(object_name, method, self, options).render
end
It appears that many gems were affected by this change in the process of upgrading to Rails 4. So if this error is coming from a gem, check to see if a Rails 4 version is available.
If this code is something you wrote, then you can attempt to use the new Tags class depending on what exactly you're trying to accomplish.

Adding nonce ruby

Ruby nonce throwing error
require 'date'
nonce = DateTime.now.to_i
Error:
undefined method `to_i' for #<DateTime:0x000000015336e8> (NoMethodError)
Working in my console it gives correct value
2.1.0 :014 > nonce = DateTime.now.to_i
=> 1405065242
Why it throws error programatically?
EDIT
Is there any way to add nonce. The condition is it should be a integer that has to be incremented on every subsequent request
As Pavan sais, if you run your code in irb it probably will not working anymore.
According to the doc, Ruby hasn't method to_i in DateTime class.
However, Rails override DateTime class to have a to_i method:
So, I think you run the command whick works in an Ruby On Rails environment, that's why it works. But if you run in a Ruby environment without Rails, it will not work.
Hope it helps.

Rails/Ruby send method can't build paths with params?

So i found this strange anomaly while working in a gem that we are using internally.
We have this private method
private
def redirect_to_element(element, next_upload)
send("scorecard_#{element.base_class_name.underscore}_path", current_scorecard, current_tab(element, next_upload))
end
Which just builds a path dynamically depending on what element is passed to it. What i would like to do is have those dynamic paths pass some params. But i get this error
undefined method `scorecard_enterprise_development_path(ignore_tracking: true)' for #<#<Class:0x007ff767a702e0>:0x007ff767899a20>
so in the console i tried several things and this is what i found.
>> scorecard_enterprise_development_path
=> "/scorecards/338/enterprise_development"
>> send('scorecard_enterprise_development_path')
=> "/scorecards/338/enterprise_development"
>> scorecard_enterprise_development_path(ignore_tracking: true)
=> "/scorecards/338/enterprise_development?ignore_tracking=true"
>> send('scorecard_enterprise_development_path(ignore_tracking: true)')
!! #<NoMethodError: undefined method `scorecard_enterprise_development_path(ignore_tracking: true)' for #<#<Class:0x007ff767a702e0>:0x007ff767899a20>>
That using the send method to build a path with params will fail. Can anyone explain why this happens?
I am using, ruby -v 1.9.3p327 and rails -v 3.2.16
#send will invoke the method identified by the first argument and pass it any arguments specified.
So you should use the method this way:
send('scorecard_enterprise_development_path', ignore_tracking: true)
See the send documentation

Squeel working in Rails Console but not in Controller. Error: undefined method `method' for nil:NilClass

I'm using Squeel, and I have an odd problem. The following line runs just fine in Rails Console, but gives an error when run in the model's controller!
Issue.joins{property.users}.where{property.users.id == 1}
Issue belongs_to Property, and Property has_and_belongs_to_many Users. I'm trying to load all the issues associated with the user_id == 1.
I get the error: "undefined method `users' for nil:NilClass" when I run the line in the IssuesController, but it works in Rails Console. What's wrong?
P.S. I'm running Rails 4.0.3, Ruby 2.0.0, and Squeel 1.1.1
Try this
my_properties = Property.where{user_id == 1}.select{issue_id}
Issue.where{id.in(my_properties)}

Can't create users in rails when using authlogic because of cookies

So, I have a standard rails 3 app with authlogic. If I'm not in the browser (in the console or in the test environment), I can't create user models.
For example:
I have this code either in a rspec test or in my console:
user = User.create(...user attributes...)
And I get this error:
NoMethodError: undefined method `cookies' for main:Object
I've looked all over the internet and can't figure this out. Any help would be greatly appreciated.
EDIT:
So, I looked around some more and found in the documentation to do this:
include Authlogic::TestCase
but then I get this error:
uninitialized constant Authlogic::TestCase
I had this same exact problem. Where in your application did you put the following line:
Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(self)
I ran into this error when I placed the line in either the environment.rb file or within a file within my initializer folder.
I eliminated the problem when I placed the line in my ApplicationController instead.

Resources