Undefined method `year' for 1:Fixnum - ruby

When using asset_sync with a Rails application I set custom headers with this line:
config.custom_headers = { '.*' => { cache_control: 'max-age=315576000', expires: 1.year.from_now.httpdate } }
Doing the same in a Sinatra app throws a 'undefined method `year' for 1:Fixnum'. What can I use instead or how do I specify 1 year from now? The year method is available as it's just Ruby code, I think it's the '1' that's causing the problem.

Yes #year is a helper method available in Rails.. Not in Sinatra.. In core Ruby Fixnum class there is no method called Fixnum#year.
Use as below :
Date.today.next_year
Date#next_year method exist in pure Ruby.

Related

undefined method constantize in rake task

I'm trying to run this code"
FACTORY = %w(ProcessedTransaction Chargeback).freeze
FACTORY.constantize.each do |factory|
factory.public_send(:delete_all)
end
end
But I get this error: NoMethodError: undefined methodconstantize' for #`
Do you know how I can solve the issue?
In ruby uppercased variables are constants by default so you can't call constantize on it. In your case it's just an array so this should work:
FACTORY = %w(ProcessedTransaction Chargeback).freeze
FACTORY.each do |factory|
factory.constantize.public_send(:delete_all)
end
You can call String#constantize only on strings but you are calling it on array FACTORY.
Remove FACTORY.constantize and add factory.constantize.public_send(:delete_all)
Also make sure you have ActiveSupport::Inflector required

Rails 4.0.3 Active resource

s=Food.find("pizza") returns information as <
<#Food:0xcee080c #attributes={"a=>"5"},"food_groups"=>[#Food::FoodGroup:0xcee0028 #attributes = {"gname="pizzatype"}]
category is an instance method available in FoodGroup. When i access the following in rails console as s.food_groups.category
NoMethodError: undefined method `category' for Food::FoodGroup:0xcee080c
Why did Rails add the dependent class at the front. How can I access the FoodGroup category method?
Here s.food_groups returns a array.
So you have to call like s.food_groups.first.category

rails method chaining context

I have what is probably a basic Q, but it appears complex in the setup. I have a module that has some classes. One class contains methods for API calls. Other classes describe a server. Dev for instance has its attributes. The server classes inherit the class that contains all the API calls. I use an instance of the server class to use one of these methods and then apply EventMachine methods to it. Here's a subset of a server class:
class PulseDev < ApiMethods
def base_uri
"http://myserver.com/api"
end
end
And an action in the methods class:
Class ApiMethods
def get_json_api_post_response(url, post_obj={})
http = EM::Synchrony.sync EventMachine::HttpRequest.new(self.base_uri+"#{url}").post(:body => post_obj)
process_response self.class.post(url, :body => post_obj).body
end
def process_response(result)
response = ActiveSupport::JSON.decode(result)
if response["code"].to_i == 200
ToolResult.new(true, response["result"], 200)
else
ToolResult.new(false, response["result"], response["code"])
end
end
end
Class ToolResult < Struct.new(:success, :result, :code)
end
And my invocation of it in the controller:
http = ApiMethods::Dev.new.get_json_api_post_response('/handshake', #j)
OK, my error is undefined method `post' for ApiMethods::PulseDev:Class
and it points to the post in my get_json_api_post_response method.
My question is: I get that it's within the context of the ApiMethods::Dev which is why self.base_uri works but how should I handle the post inside that process_response method so that it's tied to EventMachine? Is there something about method chaining I'm not seeing? In the error output I can verify that http is showing the EventMachine object so the new method seems to be working. Thanks for your time, sam
The answer is to look more carefully at the error msg. The process_response method is the one actually calling the EventMachine method and processing its :body. So it was written with an unneeded call.

Working with distance_of_time_in_words in Rails 3

I am trying to make use of Rail's distance_of_time_in_words helper but I'm getting an Undefined Method Error for some reason. Here's my code:
def confirm_has_quota
last_upload = current_user.photos.last.created_at
remaining_time = distance_of_time_in_words(1.day.ago, last_upload)
if last_upload < 1.day.ago
return true
else
flash[:error] = "You are allowed 1 upload per day. Please try again in" + remaining_time + "."
redirect_to(:back)
end
end
Which gives me "undefined method `distance_of_time_in_words'". Anyone see what I'm doing wrong here? Thanks.
The distance_of_time_in_words method is an ActionView Helper, thus needs to be called from the View (not the controller).
http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html
Or you can access the same through the view_context which is available inside the controllers.
view_context.distance_of_time_in_words

How does rescue_action in Rails3 work?

I would like to implement this
class SecurityTransgression < StandardError; end
def create
raise SecurityTransgression unless ...
end
class ApplicationController < ActionController::Base
def rescue_action(e)
case e
when SecurityTransgression
head :forbidden
end
end
end
from the this blogpost.
The problem is it does not work. I dont see a forbidden page but standard Rails error page "SecurityViolation in MyController#action". I digged that some rescue_action methods works only in the production mode. I tried that and it is the same. No change.
My question: is there any good documentation of the rescue_action method (and others)? Does this work under Rails 3.0? Because it seems this is some old
Take a look at rescue_from at the API documentation.
The rescue_action method is normally called internally with the #_env hash being passed as a parameter. The method is expecting the Exception instance to exist within the "action_dispatch.rescue.exception" key.
If you must use the rescue_action method directly, you can do the following:-
#_env[ "action_dispatch.rescue.exception" ] = exception
rescue_action( #_env )
or even more simple:-
rescue_action( { "action_dispatch.rescue.exception" => exception } )

Resources