Rails wrong number of arguments (1 for 2) in controller - ruby-on-rails

I'm trying to make a simple calculator that converts farenheit to celsius, using Ruby on Rails. I keep getting the error "Wrong number of arguments (1 for 2)" after I enter some input value. This app has been alot more difficult than it should be. I've been fixing errors as they come up, but can't figure this one out. Other posts I've read are much different and more elaborate than this simple app I'm trying to make. Thanks for any help.
The controller ...
class CalculatorController < ApplicationController
def calculate
#farenheit = params[:temperature]
unless #farenheit.blank?
#farenheit = Temperature.calculate({ :farenheit => #farenheit})
end
#celsius = (#farenheit - 32) * (5.0 / 9.0)
end
private
## Strong Parameters
def user_params
params.require(:farenheit)
end
end
The model...
class Temperature < ActiveRecord::Base
# attr_accessible :farenheit
validates_presence_of :farenheit
validates_numericality_of :farenheit
end
The view...
<h1>Temperature Calculator</h1>
<%= form_tag(calculator_calculate_path, method: "get", action: "calculate") do |form|%>
<p>Please enter a temperature in degrees Farenheit</p>
<%= text_field_tag 'temperature', #farenheit %></p>
<%= submit_tag 'Convert' %>
<h2>Result: </h2>
<h3> <% #celsius %> </h3>
<% end %>
The stack trace (first 10 lines)...
activerecord (4.1.6) lib/active_record/relation/calculations.rb:109:in `calculate'
C:in `calculate'
app/controllers/calculator_controller.rb:6:in `calculate'
actionpack (4.1.6) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.1.6) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.1.6) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.1.6) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.1.6) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.6) lib/active_support/callbacks.rb:113:in `call'

You've defined an instance method calculate in your CalculatorController, but calculate is also a class method defined in ActiveRecord. It is the ActiveRecord class method that you are calling when you say
#farenheit = Temperature.calculate({ :farenheit => #farenheit})
and that method requires at least two arguments. Here are the docs:
calculate(operation, column_name, options = {}) public
This calculates aggregate values in the given column. Methods for
count, sum, average, minimum, and maximum have been added as
shortcuts. Options such as :conditions, :order, :group, :having, and
:joins can be passed to customize the query.
See http://apidock.com/rails/ActiveRecord/Calculations/ClassMethods/calculate
I'm not sure why you're using the calculate class method though. Why not use only your calculation of #celcius? The following should get your calculate method working without an ArgumentError:
def calculate
#farenheit = params[:temperature]
unless #farenheit.blank?
#celsius = (#farenheit.to_i - 32) * (5.0 / 9.0)
end
end

Related

Active Admin Show Page for one model is not working

I am integrating Active Admin into a Ruby on Rails app. I registered all my models and already set up index, filter and show for all the models. Everything is working, but for one model the admin/show page is not running.
When calling the show page from the admin/index page, I get:
NoMethodError in Admin/safts#show
Showing /Users/xxxxxx/.rvm/gems/ruby-1.8.7-p374#xxxxxx/gems/activeadmin-0.6.0/app/views/active_admin/resource/show.html.arb where line #1 raised:
undefined method `empty?' for #<Keyword:0x105498800>
Extracted source (around line #1):
1: insert_tag renderer_for(:show)
Request
Parameters:
{"id"=>"9"}
The relative entry in my log file is:
Started GET "/admin/safts/9" for 127.0.0.1 at Sun Feb 25 14:48:04 +0100 2018
Processing by Admin::SaftsController#show as HTML
Parameters: {"id"=>"9"}
[1m[35mAdminUser Load (0.6ms)[0m SELECT `admin_users`.* FROM `admin_users` WHERE `admin_users`.`id` = 2 LIMIT 1
[1m[36mSaft Load (0.2ms)[0m [1mSELECT `safts`.* FROM `safts` WHERE `safts`.`id` = ? LIMIT 1[0m [["id", "9"]]
[1m[35mKeyword Load (0.3ms)[0m SELECT `keywords`.* FROM `keywords` WHERE `keywords`.`id` = 138 LIMIT 1
Rendered /Users/xxxxxx/.rvm/gems/ruby-1.8.7-p374#xxxxxx/gems/activeadmin-0.6.0/app/views/active_admin/resource/show.html.arb (3.1ms)
Completed 500 Internal Server Error in 10ms
ActionView::Template::Error (undefined method `empty?' for #<Keyword:0x1052a0890>):
1: insert_tag renderer_for(:show)
activemodel (3.2.5) lib/active_model/attribute_methods.rb:407:in `method_missing'
activerecord (3.2.5) lib/active_record/attribute_methods.rb:149:in `method_missing'
activeadmin (0.6.0) lib/active_admin/views/pages/show.rb:38:in `default_title'
activeadmin (0.6.0) lib/active_admin/views/pages/show.rb:14:in `title'
activeadmin (0.6.0) lib/active_admin/views/pages/base.rb:25:in `build_active_admin_head'
arbre (1.0.1) lib/arbre/context.rb:92:in `with_current_arbre_element'
arbre (1.0.1) lib/arbre/element/builder_methods.rb:49:in `within'
activeadmin (0.6.0) lib/active_admin/views/pages/base.rb:24:in `build_active_admin_head'
activeadmin (0.6.0) lib/active_admin/views/pages/base.rb:9:in `build'
arbre (1.0.1) lib/arbre/element/builder_methods.rb:30:in `build_tag'
arbre (1.0.1) lib/arbre/context.rb:92:in `with_current_arbre_element'
arbre (1.0.1) lib/arbre/element/builder_methods.rb:26:in `build_tag'
arbre (1.0.1) lib/arbre/element/builder_methods.rb:39:in `insert_tag'
activeadmin (0.6.0) app/views/active_admin/resource/show.html.arb:1:in `___sers__tephan__rvm_gems_ruby_______p____somesite_gems_activeadmin_______app_views_active_admin_resource_show_html_arb___1026777847_2195984600'
arbre (1.0.1) lib/arbre/context.rb:45:in `instance_eval'
arbre (1.0.1) lib/arbre/context.rb:45:in `initialize'
activeadmin (0.6.0) app/views/active_admin/resource/show.html.arb:1:in `new'
activeadmin (0.6.0) app/views/active_admin/resource/show.html.arb:1:in `___sers__tephan__rvm_gems_ruby_______p____somesite_gems_activeadmin_______app_views_active_admin_resource_show_html_arb___1026777847_2195984600'
actionpack (3.2.5) lib/action_view/template.rb:145:in `send'
actionpack (3.2.5) lib/action_view/template.rb:145:in `render'
activesupport (3.2.5) lib/active_support/notifications.rb:125:in `instrument'
.
.
.
script/rails:6:in `gem_original_require'
script/rails:6:in `require'
script/rails:6
Rendered /Users/xxxxxx/.rvm/gems/ruby-1.8.7-p374#xxxxxx/gems/actionpack-3.2.5/lib/action_dispatch/middleware/templates/rescues/_trace.erb (0.7ms)
Rendered /Users/xxxxxx/.rvm/gems/ruby-1.8.7-p374#xxxxxx/gems/actionpack-3.2.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.6ms)
Rendered /Users/xxxxxx/.rvm/gems/ruby-1.8.7-p374#xxxxxx/gems/actionpack-3.2.5/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (9.8ms)
The rails model is:
class Saft < ActiveRecord::Base
attr_accessible :colour, :cover_alt, :description, :number, :short, :title_id
# Associations
has_and_belongs_to_many :keywords, :join_table => "safts_keywords"
has_many :authors, :through => :texts
has_many :texts
belongs_to :title, :class_name => "Keyword", :foreign_key => "title_id"
has_one :cover
has_many :stamps
has_many :images
end
The ActiveAdmin resource is:
ActiveAdmin.register Saft do
index do
column "Issue", :number
column "Title", :title_id do |saft|
link_to saft.title.word.capitalize, saft_path(saft)
end
column :short
column :description
column :colour
column :cover_alt
default_actions
end
# Filter only by:
filter :title_id, :label => 'Title', :as => :select, :collection => Saft.all.map{|u| ["#{u.title.word.capitalize}", u.id]}
filter :short
form do |f|
f.inputs "Saft Details" do
f.input :number, :label => "Number of issue"
f.input :title_id, :label => 'Title', :as => :select, :collection => Keyword.all.map{|u| ["#{u.word.capitalize}", u.id]}
f.input :short
f.input :description
f.input :colour, :label => "Colour (in hex)"
f.input :cover_alt
end
f.actions
end
show do
panel "Saft Details" do
attributes_table_for saft do
row :id
row :number
row :title_id
row :short
row :description
row :colour
row :cover_alt
row :created_at
row :updated_at
end
end
active_admin_comments
end
end
Just for context: SAFT is a magazine, with texts, images, authors, etc. All the other resources are working well in Admin. Only the show page of SAFT is not working. What could it be?
Your log entry says Keyword does not respond to empty? in Pages::Show#default_title.
default_title calls display_name, have you overridden display_name_methods? In this case you can remove :title
In config/initializers/active_admin:
config.display_name_methods = [ :display_name,
:full_name,
:name,
:username,
:login,
:email,
:to_s ]
Perhaps better in this case you can also try setting the title explicitly, eg.
show title: :short do ...
I notice you are on very old versions of Rails and ActiveAdmin, I hope you will consider upgrading.
I was able to solve the problem, which is related to the associations.
The DisplayHelper Module of the gem has to be modified as follows:
module ActiveAdmin
module ViewHelpers
module DisplayHelper
# Attempts to call any known display name methods on the resource.
# See the setting in `application.rb` for the list of methods and their priority.
def display_name(resource)
resource.send display_name_method_for resource
end
# Looks up and caches the first available display name method.
def display_name_method_for(resource)
##display_name_methods_cache ||= {}
##display_name_methods_cache[resource.class] ||= begin
methods = active_admin_application.display_name_methods - association_methods_for(resource)
methods.detect{ |method| resource.respond_to? method }
end
end
# To prevent conflicts, we exclude any methods that happen to be associations.
def association_methods_for(resource)
return [] unless resource.class.respond_to? :reflect_on_all_associations
resource.class.reflect_on_all_associations.map(&:name)
end
# Return a pretty string for any object
# Date Time are formatted via #localize with :format => :long
# ActiveRecord objects are formatted via #auto_link
# We attempt to #display_name of any other objects
def pretty_format(object)
case object
when String
object
when Arbre::Element
object
when Date, Time
localize(object, :format => :long)
when ActiveRecord::Base
auto_link(object)
else
display_name(object)
end
end
end
end
end
This is because in the SAFT model there is an association with the name title, which goes into conflict with the display_name_method.

Using Rails ActionView/Controller completely outside rails

I learnt a bit of basic Rails and one of the attempts I'm making now is to use ActionController and ActionView completely outside of Rails (in a non rails app).
So far what I have is the following example after installing actionpack-4.0.2
require 'action_view'
require 'action_controller'
class SimpleController < AbstractController::Base
include AbstractController::Rendering
include AbstractController::Layouts
include AbstractController::Helpers
include AbstractController::Translation
include AbstractController::AssetPaths
include ActionController::UrlFor
include ActionDispatch::Routing::UrlFor
# All the .html.erb files are placed in the folder Views
self.view_paths = "Views"
def say_hello
render_to_string template: "Hello"
end
def create_person
render_to_string template: "Form"
end
end
c = SimpleController.new
puts c.say_hello
puts c.create_person
Hello.html.erb (To be placed in the Views folder)
<html>
<body>
<p>
<%= highlight( 'This is Hello world', 'Hello world') %>
</p>
</body>
</html>
Form.html.erb (To be placed in the Views folder)
<html>
<body>
<%= form_for( :person, :url => { :action => 'create' } ) do |f| %>
<%= f.text_field :first_name %>
<%= f.text_field :last_name %>
<%= f.password_field :password %>
<%= submit_tag 'Create' %>
<% end %>
</body>
</html>
Below is the output of the above program
<html>
<body>
<p>
This is <mark>Hello world</mark>
</p>
</body>
</html>
C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-4.0.2/lib/action_view/helpers/url
_helper.rb:38:in `url_for': arguments passed to url_for can't be handled. Please
require routes or provide your own implementation (ActionView::Template::Error)
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-4.0.2/lib/action_vie
w/helpers/form_tag_helper.rb:729:in `block in html_options_for_form'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-4.0.2/lib/action_vie
w/helpers/form_tag_helper.rb:725:in `tap'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-4.0.2/lib/action_vie
w/helpers/form_tag_helper.rb:725:in `html_options_for_form'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-4.0.2/lib/action_vie
w/helpers/form_tag_helper.rb:67:in `form_tag'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-4.0.2/lib/action_vie
w/helpers/form_helper.rb:438:in `form_for'
from ./Views/Form.html.erb:4:in `_____etho
ds__uby_html___rototype__iews__orm_html_erb__678800263_20873052'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-4.0.2/lib/action_vie
w/template.rb:143:in `block in render'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-4.0.2/lib/active_
support/notifications.rb:161:in `instrument'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-4.0.2/lib/action_vie
w/template.rb:141:in `render'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-4.0.2/lib/action_vie
w/renderer/template_renderer.rb:49:in `block (2 levels) in render_template'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-4.0.2/lib/action_vie
w/renderer/abstract_renderer.rb:38:in `block in instrument'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-4.0.2/lib/active_
support/notifications.rb:159:in `block in instrument'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-4.0.2/lib/active_
support/notifications/instrumenter.rb:20:in `instrument'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-4.0.2/lib/active_
support/notifications.rb:159:in `instrument'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-4.0.2/lib/action_vie
w/renderer/abstract_renderer.rb:38:in `instrument'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-4.0.2/lib/action_vie
w/renderer/template_renderer.rb:48:in `block in render_template'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-4.0.2/lib/action_vie
w/renderer/template_renderer.rb:56:in `render_with_layout'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-4.0.2/lib/action_vie
w/renderer/template_renderer.rb:47:in `render_template'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-4.0.2/lib/action_vie
w/renderer/template_renderer.rb:17:in `render'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-4.0.2/lib/action_vie
w/renderer/renderer.rb:42:in `render_template'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-4.0.2/lib/action_vie
w/renderer/renderer.rb:23:in `render'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-4.0.2/lib/abstract_c
ontroller/rendering.rb:127:in `_render_template'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-4.0.2/lib/abstract_c
ontroller/rendering.rb:120:in `render_to_body'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-4.0.2/lib/abstract_c
ontroller/rendering.rb:113:in `render_to_string'
from sample.rb:21:in `edit_person'
from sample.rb:28:in `<main>'
The say_hello method works fine with out any issues and I can use most of the ActiveView ViewHelpers in the erb files (Hello.html.erb above uses one such highlight method). So this is a way with which I can generate static html pages by using power of ActionView.
But I'm not able to get the edit_person work as it uses form_for helper and this needs custom url_for implementation or rails standard routes.rb implementation (as prompted in the error message). I would like to get this working with routes.rb approach but I get the impression that I need Rails::Application object and some url_helpers etc (so far I couldn't get it working). Is there a way to do this?
Then later I would like integrate this solution with Webrick to process the requests from browser (when the user creates a person).
Please note this is a attempt to learn some details of Rails and not meant as a solution to any problem that you cannot do with using standard rails framework. Any help would be greatly appreciated.
Not an answer but just for some related code.
To simplify things a bit and I know it still errors but you get some data back in the errors.
I'll show a portion of it after the code.
require 'action_controller'
class SimpleController < ActionController::Base
def say_hello
p render_to_string template: "Hello"
end
def create_person
p render_to_string template: "Form"
end
end
c = SimpleController.new
puts c.say_hello
puts c.create_person
Skipping the views handler for now.
OUT:
> ruby sample.rb
/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/actionview- 4.1.8/lib/action_view/path_set.rb:46:in `find': Missing template /Hello with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby]}. Searched in: (ActionView::MissingTemplate)
The top should be good enough as it just trees up to that anyway.
Now a run with the class views.
We get similar results:
OUT:
ruby sample.rb
"\n \n \n This is Hello world\n \n \n"
<html>
<body>
<p>
This is <mark>Hello world</mark>
</p>
</body>
</html>
.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/actionview-4.1.8/lib/action_view/helpers/url_helper.rb:38:in `url_for': arguments passed to url_for can't be handled. Please require routes or provide your own implementation (ActionView::Template::Error)
Rails 5 introduced the ActionController::Renderer that makes it a bit more convenient to use the Rails template system outside of Rails.
Example:
renderer = ApplicationController.renderer
content = renderer.render template: 'ruby_template'
(from the Tests)
It's somewhat useful because plain ERB is severely limited. I'm using it in a generator where life without partials would be inconvenient.

Upgraded to Rails 2.3.11 and am getting an Uninitialized Constant::AWS error

I am trying to use our app to generate screenshots. However, instead I generate the following:
NameError in Step_results#show
Showing app/views/step_results/show.html.haml where line #26 raised:
uninitialized constant StepResult::AWS
Extracted source (around line #26):
23: %p{ :title => #step_result.message }= h #step_result.message
24: = link_to 'close window', '#', :onclick => 'window.close();'
25: = link_to 'Screenshot', html_screen_shot_src(#step_result), :class => 'screenshot', :target => "frame", :id => "screenshot", :onclick => '$("a#screenshot").hide(); $("a#screenscrape").show()' unless html_screen_shot_src(#step_result).nil?
26: = link_to 'Screen Scrape', html_screen_scrape_src(#step_result), :class => 'screenscrape', :target => "frame", :id => "screenscrape", :onclick => '$("a#screenscrape").hide(); $("a#screenshot").show()'
27:
28: #frame
29: %iframe{ :src => html_screen_scrape_src(#step_result), :name => 'frame' }
/Users/jasonbodak/.rvm/gems/ree-1.8.7-2011.03#janova-local_app/gems/activesupport-2.3.11/lib/active_support/dependencies.rb:131:in `const_missing'
/Users/jasonbodak/janova/janova/app/models/step_result.rb:73:in `html_scrape_url'
/Users/jasonbodak/janova/janova/app/helpers/step_results_helper.rb:3:in `html_screen_scrape_src'
/Users/jasonbodak/janova/janova/app/views/step_results/show.html.haml:26:in `_run_haml_app47views47step_results47show46html46haml'
/Users/jasonbodak/.rvm/gems/ree-1.8.7-2011.03#janova-local_app/gems/actionpack-2.3.11/lib/action_view/renderable.rb:34:in `send'
/Users/jasonbodak/.rvm/gems/ree-1.8.7-2011.03#janova-local_app/gems/actionpack-2.3.11/lib/action_view/renderable.rb:34:in `render'
/Users/jasonbodak/.rvm/gems/ree-1.8.7-2011.03#janova-local_app/gems/actionpack-2.3.11/lib/action_view/base.rb:306:in `with_template'
/Users/jasonbodak/.rvm/gems/ree-1.8.7-2011.03#janova-local_app/gems/actionpack-2.3.11/lib/action_view/renderable.rb:30:in `render'
/Users/jasonbodak/.rvm/gems/ree-1.8.7-2011.03#janova-local_app/gems/actionpack-2.3.11/lib/action_view/template.rb:205:in `render_template'
/Users/jasonbodak/.rvm/gems/ree-1.8.7-2011.03#janova-local_app/gems/actionpack-2.3.11/lib/action_view/base.rb:265:in `render_without_haml'
/Users/jasonbodak/.rvm/gems/ree-1.8.7-2011.03#janova-local_app/gems/haml-3.0.22/lib/haml/helpers/action_view_mods.rb:13:in `render'
/Users/jasonbodak/.rvm/gems/ree-1.8.7-2011.03#janova-local_app/gems/actionpack-2.3.11/lib/action_controller/base.rb:1252:in `render_for_file'
/Users/jasonbodak/.rvm/gems/ree-1.8.7-2011.03#janova-local_app/gems/actionpack-2.3.11/lib/action_controller/base.rb:978:in `render_without_benchmark'
/Users/jasonbodak/.rvm/gems/ree-1.8.7-2011.03#janova-local_app/gems/actionpack-2.3.11/lib/action_controller/benchmarking.rb:51:in `render'
/Users/jasonbodak/.rvm/gems/ree-1.8.7-2011.03#janova-local_app/gems/activesupport-2.3.11/lib/active_support/core_ext/benchmark.rb:17:in `ms'
/Users/jasonbodak/.rvm/gems/ree-1.8.7-2011.03#janova-local_app/gems/activesupport-2.3.11/lib/active_support/core_ext/benchmark.rb:17:in `ms'
/Users/jasonbodak/.rvm/gems/ree-1.8.7-2011.03#janova-local_app/gems/actionpack-2.3.11/lib/action_controller/benchmarking.rb:51:in `render'
/Users/jasonbodak/janova/janova/app/controllers/step_results_controller.rb:4:in `show'
/Users/jasonbodak/.rvm/gems/ree-1.8.7-2011.03#janova-local_app/gems/actionpack-2.3.11/lib/action_controller/base.rb:1333:in `send'
/Users/jasonbodak/.rvm/gems/ree-1.8.7-2011.03#janova-local_app/gems/actionpack-2.3.11/lib/action_controller/base.rb:1333:in `perform_action_without_filters'
/Users/jasonbodak/.rvm/gems/ree-1.8.7-2011.03#janova-local_app/gems/actionpack-2.3.11/lib/action_controller/filters.rb:617:in `call_filters'
/Users/jasonbodak/.rvm/gems/ree-1.8.7-2011.03#janova-local_app/gems/actionpack-2.3.11/lib/action_controller/filters.rb:610:in `perform_action_without_benchmark'
/Users/jasonbodak/.rvm/gems/ree-1.8.7-2011.03#janova-local_app/gems/actionpack-2.3.11/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
/Users/jasonbodak/.rvm/gems/ree-1.8.7-2011.03#janova-local_app/gems/activesupport-2.3.11/lib/active_support/core_ext/benchmark.rb:17:in `ms'
/Users/jasonbodak/.rvm/gems/ree-1.8.7-2011.03#janova-local_app/gems/activesupport-2.3.11/lib/active_support/core_ext/benchmark.rb:17:in `ms'
/Users/jasonbodak/.rvm/gems/ree-1.8.7-2011.03#janova-local_app/gems/actionpack-2.3.11/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
/Users/jasonbodak/.rvm/gems/ree-1.8.7-2011.03#janova-local_app/gems/actionpack-2.3.11/lib/action_controller/rescue.rb:160:in `perform_action_without_flash'
/Users/jasonbodak/.rvm/gems/ree-1.8.7-2011.03#janova-local_app/gems/actionpack-2.3.11/lib/action_controller/flash.rb:151:in `perform_action'
/Users/jasonbodak/.rvm/gems/ree-1.8.7-2011.03#janova-local_app/gems/actionpack-2.3.11/lib/action_controller/base.rb:532:in `send'
/Users/jasonbodak/.rvm/gems/ree-1.8.7-2011.03#janova-local_app/gems/actionpack-2.3.11/lib/action_controller/base.rb:532:in `process_without_filters'
/Users/jasonbodak/.rvm/gems/ree-1.8.7-2011.03#janova-local_app/gems/actionpack-2.3.11/lib/action_controller/filters.rb:606:in `process'
/Users/jasonbodak/.rvm/gems/ree-1.8.7-2011.03#janova-local_app/gems/actionpack-2.3.11/lib/action_controller/base.rb:391:in `process'
/Users/jasonbodak/.rvm/gems/ree-1.8.7-2011.03#janova-local_app/gems/actionpack-2.3.11/lib/action_controller/base.rb:386:in `call'
/Users/jasonbodak/.rvm/gems/ree-1.8.7-2011.03#janova-local_app/gems/actionpack-2.3.11/lib/action_controller/routing/route_set.rb:438:in `call'
Any ideas why I am suddenly getting an Amazon Worker Service error now? Thanks.
Edit: Here is the code for the html_scrape_url method:
def html_scrape_url
tries = 0
begin
return nil if html_filename.blank?
AWS::S3::S3Object.url_for(html_filename, s3_bucket)
rescue AWS::S3::NoConnectionEstablished => e
if tries < 1
logger.debug "S3 connection not established. Establishing..."
AWS::S3::Base.establish_connection!(
:access_key_id => APP_CONFIG[:amazon_access_key_id],
:secret_access_key => APP_CONFIG[:amazon_secret_access_key]
)
tries += 1
retry
end
end
end
OK, this is very weird, but adding
require 'aws/s3'
to the top of my model (called step_result.rb, as you can see above) fixed the issue. I must admit that I do not understand how doing that fixed it... I just tried it on a whim, but I'm glad I did.

stack level to deep Rails 2.3.14

I'm developing a plugin for a management framework and when I start the webrick server in the development mode, a strange error raises(stack level too deep). It happens when an action (e.g. the show action) starts to render a template. Unfortunately i have no idea why this is happen.
SystemStackError in Stories#show
Showing vendor/plugins/stories/app/views/stories/show.rhtml where line #5 raised:
stack level too deep
Extracted source (around line #5):
5: link_to_if_authorized 'aa', {:controller => "stories", :action => "index", :id => #story.id, :project_id => #story.project.id}, :title => l(:view_story), :class => 'icon icon-zoom-out'
6: link_to_if_authorized 'bb', {:controller => "stories", :action => "edit", :id => #story.id, :project_id => #story.project.id}, :title => l(:button_edit), :class => 'icon icon-edit'
7: link_to 'bb', {:id => #story.id, :project_id => #story.project.id}, :confirm => 'Really delete?', :method => :delete, :class => 'icon icon-del' if User.current.allowed_to? (:delete_stories, #project)
RAILS_ROOT: /home/haendwic/Documents/Aptana Studio 3 Workspace/1.4-stable-SVN
Application Trace | Framework Trace | Full Trace
/var/lib/gems/1.8/gems/activerecord-2.3.14/lib/active_record/attribute_methods.rb:248:in method_missing'
/var/lib/gems/1.8/gems/activerecord-2.3.14/lib/active_record/attribute_methods.rb:249:in method_missing'
/var/lib/gems/1.8/gems/activerecord-2.3.14/lib/active_record/associations /association_proxy.rb:215:in send'
/var/lib/gems/1.8/gems/activerecord-2.3.14/lib/active_record/associations/association_proxy.rb:215:in method_missing'
/home/haendwic/Documents/Aptana Studio 3 Workspace/1.4-stable-SVN/vendor/plugins/stories/app/views/stories/show.rhtml:5:in _run_rhtml_vendor47plugins47stories47app47views47stories47show46rhtml'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_view/renderable.rb:34:in send'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_view/renderable.rb:34:in render'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_view/base.rb:306:in with_template'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_view/renderable.rb:30:in render'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_view/template.rb:205:in render_template'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_view/base.rb:265:in render'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_view/base.rb:348:in _render_with_layout'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_view/base.rb:262:in render'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/base.rb:1252:in render_for_file'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/base.rb:936:in render_without_benchmark'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/benchmarking.rb:51:in render'
/var/lib/gems/1.8/gems/activesupport-2.3.14/lib/active_support/core_ext/benchmark.rb:17:in ms'
/usr/lib/ruby/1.8/benchmark.rb:308:in realtime'
/var/lib/gems/1.8/gems/activesupport-2.3.14/lib/active_support/core_ext/benchmark.rb:17:in ms'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/benchmarking.rb:51:in render'
/home/haendwic/Documents/Aptana Studio 3 Workspace/1.4-stable-SVN/vendor/plugins/stories/app/controllers/stories_controller.rb:104:in show'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/mime_responds.rb:135:in call'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/mime_responds.rb:135:in custom'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/mime_responds.rb:179:in call'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/mime_responds.rb:179:in respond'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/mime_responds.rb:173:in each'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/mime_responds.rb:173:in respond'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/mime_responds.rb:107:in respond_to'
/home/haendwic/Documents/Aptana Studio 3 Workspace/1.4-stable-SVN/vendor/plugins/stories/app/controllers/stories_controller.rb:102:in show'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/base.rb:1333:in send'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/base.rb:1333:in perform_action_without_filters'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/filters.rb:617:in call_filters'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/filters.rb:610:in perform_action_without_benchmark'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/benchmarking.rb:68:in perform_action_without_rescue'
/var/lib/gems/1.8/gems/activesupport-2.3.14/lib/active_support/core_ext/benchmark.rb:17:in ms'
/usr/lib/ruby/1.8/benchmark.rb:308:in realtime'
/var/lib/gems/1.8/gems/activesupport-2.3.14/lib/active_support/core_ext/benchmark.rb:17:in ms'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/benchmarking.rb:68:in perform_action_without_rescue'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/rescue.rb:160:in perform_action_without_flash'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/flash.rb:151:in perform_action'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/base.rb:532:in send'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/base.rb:532:in process_without_filters'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/filters.rb:606:in process'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/base.rb:391:in process'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/base.rb:386:in call'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/routing/route_set.rb:438:in call'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/dispatcher.rb:87:in dispatch'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/dispatcher.rb:121:in _call'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/dispatcher.rb:130:in build_middleware_stack'
/var/lib/gems/1.8/gems/activerecord-2.3.14/lib/active_record/query_cache.rb:29:in call'
/var/lib/gems/1.8/gems/activerecord-2.3.14/lib/active_record/query_cache.rb:29:in call'
/var/lib/gems/1.8/gems/activerecord-2.3.14/lib/active_record/connection_adapters/abstract/query_cache.rb:34:in cache'
/var/lib/gems/1.8/gems/activerecord-2.3.14/lib/active_record/query_cache.rb:9:in cache'
/var/lib/gems/1.8/gems/activerecord-2.3.14/lib/active_record/query_cache.rb:28:in call'
/var/lib/gems/1.8/gems/activerecord-2.3.14/lib/active_record/connection_adapters/abstract/connection_pool.rb:361:in call'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/string_coercion.rb:25:in call'
/var/lib/gems/1.8/gems/rack-1.1.3/lib/rack/head.rb:9:in call'
/var/lib/gems/1.8/gems/rack-1.1.3/lib/rack/methodoverride.rb:24:in call'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/params_parser.rb:15:in call'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/session /cookie_store.rb:99:in call'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/failsafe.rb:26:in call'
/var/lib/gems/1.8/gems/rack-1.1.3/lib/rack/lock.rb:11:in call'
/var/lib/gems/1.8/gems/rack-1.1.3/lib/rack/lock.rb:11:in synchronize'
/var/lib/gems/1.8/gems/rack-1.1.3/lib/rack/lock.rb:11:in call'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/dispatcher.rb:114:in call'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/reloader.rb:34:in run'
/var/lib/gems/1.8/gems/actionpack-2.3.14/lib/action_controller/dispatcher.rb:108:in call'
/var/lib/gems/1.8/gems/rails-2.3.14/lib/rails/rack/static.rb:31:in call'
/var/lib/gems/1.8/gems/rack-1.1.3/lib/rack/urlmap.rb:47:in call'
/var/lib/gems/1.8/gems/rack-1.1.3/lib/rack/urlmap.rb:41:in each'
/var/lib/gems/1.8/gems/rack-1.1.3/lib/rack/urlmap.rb:41:in call'
/var/lib/gems/1.8/gems/rails-2.3.14/lib/rails/rack/log_tailer.rb:17:in call'
/var/lib/gems/1.8/gems/rack-1.1.3/lib/rack/content_length.rb:13:in call'
/var/lib/gems/1.8/gems/rack-1.1.3/lib/rack/handler/webrick.rb:48:in service'
/usr/lib/ruby/1.8/webrick/httpserver.rb:104:in service'
/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in run'
/usr/lib/ruby/1.8/webrick/server.rb:173:in start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:162:in start'
/usr/lib/ruby/1.8/webrick/server.rb:162:in start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:95:in start'
/usr/lib/ruby/1.8/webrick/server.rb:92:in each'
/usr/lib/ruby/1.8/webrick/server.rb:92:in start'
/usr/lib/ruby/1.8/webrick/server.rb:23:in start'
/usr/lib/ruby/1.8/webrick/server.rb:82:in start'
/var/lib/gems/1.8/gems/rack-1.1.3/lib/rack/handler/webrick.rb:14:in run'
/var/lib/gems/1.8/gems/rails-2.3.14/lib/commands/server.rb:111
script/server:3:in require'
script/server:3
Request
Parameters:
{"project_id"=>"1",
"id"=>"2"}
Show session dump
Response
Headers:
{"Cache-Control"=>"no-cache",
"Content-Type"=>"text/html"}
and here is the action from the controller
def show
#edit_allowed = User.current.allowed_to?(:edit_stories, #project)
respond_to do |format|
format.html {
render :template => 'stories/show'
}
format.api
format.pdf { send_data(story_to_pdf(#story), :type => 'application/pdf', :filename => "#{#project.identifier}-#{#story.id}.pdf") }
end
end
and at last a part of the view
link_to_if_authorized 'aa', {:controller => "stories", :action => "index", :id => #story.id, :project_id => #story.project.id}, :title => l(:view_story), :class => 'icon icon-zoom-out'
link_to_if_authorized 'bb', {:controller => "stories", :action => "edit", :id => #story.id, :project_id => #story.project.id}, :title => l(:button_edit), :class => 'icon icon-edit'
link_to 'bb', {:id => #story.id, :project_id => #story.project.id}, :confirm => 'Really delete?', :method => :delete, :class => 'icon icon-del' if User.current.allowed_to?(:delete_stories, #project)
Maybe it's important to say, that in the production mode the plugin is stable and routes the actions (incl. show) correctly
This is typically caused by you (or others) patching core models (probably the Project model) from a plugin without taking the rails reloader into account.
If you override methods (e.g. using alias_method_chain) and your patch is loaded twice, you can easily create an infinite loop when calling the old method.
# This is our initial class
class MyClass
def foo
puts "original foo"
end
end
module Patch
def self.included(base)
base.alias_method_chain :foo, :feature
end
def foo_with_feature
foo_without_feature # call the "original" method
puts "foo with feature"
end
end
# patch the class
MyClass.send(:include, Patch)
# Now call the patched method
MyClass.new.foo
# prints:
# original foo
# foo with feature
foo now refers to the method foo_with_feature while the original foo method now is accessible from foo_without_feature
All right, until now everything looked good. Now let's see what happens, if we load our patch again
# patch again
MyClass.send(:include, Patch)
# And call the method again
MyClass.new.foo
# SystemStackError: stack level too deep
# from (irb):7:in `foo_without_feature'
# from (irb):7:in `foo'
# from (irb):27
You see a SystemStackError caused by an infinite loop. This is because after the second loading of the patch, foo_without_feature now refers to the foo_with_feature method from the first patch. When calling it, it will over and over again call foo_without_feature until the stack is full.
You said that it only crashes on the second request. This is exactly the typical behavior when something is odd on the class reloader. By default, Rails will reload all classes on every request in development mode but only once in production mode.
Taming the rails reloader is a bit tricky sometimes. As some general guidelines, you should
use require_dependency instead of require when loading patches
load your patches using Dispatcher.to_prepare
always declare the patched classes unloadable
The most critical part is using Dispatcher.to_prepare. It is a callback which is called once in production mode and before each request in development mode and is thus the ideal place for loading patches.
As a side note though: When using Redmine 2 (or the upcoming ChiliProject 4), i.e. Rails 3, the class patching will be rather different from this approach - most probably easier.
The "stack level too deep" error means that you have a stack overflow (it happens often enough to have a site named after it). That occurs when a function calls itself indefinitely or when two functions call each other indefinitely.
Your error happens on line 5, so I'd check the source for link_to_if_authorized. Something in there is causing an infinite loop.

Rails error with validation and multiple models, can't convert HashWithIndifferentAccess into String

I haven't used rails since version 1.2 or so and a few things have changed. I have an issue where I am trying to save an empty model to get validation errors on attributes using :validates_presence_of and instead I am getting the error 'can't convert HashWithIndifferentAccess into String'. I will attempt to simplify my code to get the point across as tersely as possible...
my model:
class Project < ActiveRecord::Base
validates_presence_of :title, :description
validates_uniqueness_of :title
has_one :address
accepts_nested_attributes_for :address, :allow_destroy => true
end
Child model:
class Address < ActiveRecord::Base
validates_presence_of :title, :street
belongs_to :project
end
controller:
class ProjectsController < ApplicationController
def create
#project = Project.new(params[:project])
if #project.save
flash[:notice] = #project.title + ' successfully created'
redirect_to projects_path
else
render :action => 'new'
end
end
end
view:
<%= error_messages_for 'project' %>
<% form_for #project do |f| %>
<table width="100%" cellpadding="3" cellspacing="0">
<tr>
<td class="adminlabel">
<label for="Title">Title</label>
</td>
<td class="adminbody">
<%= f.text_field :title %>
</td>
</tr>
.....
<% f.fields_for :address do |address| %>
....
This code adds and updates just fine as long as I fill in all the required fields, if I leave any blank I get the error mentioned above, not the most graceful way of alerting users there is an issue ;)
Request params:
{"commit"=>"Save",
"project"=>{"title"=>"",
"notes"=>"",
"description"=>"",
"address_attributes"=>{"city"=>"",
"zip"=>"",
"title"=>"",
"country"=>"",
"suite"=>"",
"street"=>"",
"state"=>""}},
"authenticity_token"=>"iNPQZrf/oBv22vaI0toTGhknwx0aAU3BSvnIh6qgYQ8="}
I have been searching for days and am at my wits end, if anyone can shed a little light on this for me I would greatly appreciate it.
Thanks in advance!
Brendan
PS - the stack trace as requested:
.../app/controllers/projects_controller.rb:27:in `+'
.../app/controllers/projects_controller.rb:27:in `create'
../actionpack-2.3.4/lib/action_controller/base.rb:1331:in `send'
../actionpack-2.3.4/lib/action_controller/base.rb:1331:in `perform_action_without_filters'
../actionpack-2.3.4/lib/action_controller/filters.rb:617:in `call_filters'
../actionpack-2.3.4/lib/action_controller/filters.rb:610:in `perform_action_without_benchmark'
../actionpack-2.3.4/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
../activesupport-2.3.4/lib/active_support/core_ext/benchmark.rb:17:in `ms'
../activesupport-2.3.4/lib/active_support/core_ext/benchmark.rb:10:in `realtime'
../activesupport-2.3.4/lib/active_support/core_ext/benchmark.rb:17:in `ms'
../actionpack-2.3.4/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
../actionpack-2.3.4/lib/action_controller/rescue.rb:160:in `perform_action_without_flash'
../actionpack-2.3.4/lib/action_controller/flash.rb:146:in `perform_action'
../actionpack-2.3.4/lib/action_controller/base.rb:532:in `send'
../actionpack-2.3.4/lib/action_controller/base.rb:532:in `process_without_filters'
../actionpack-2.3.4/lib/action_controller/filters.rb:606:in `process'
../actionpack-2.3.4/lib/action_controller/base.rb:391:in `process'
../actionpack-2.3.4/lib/action_controller/base.rb:386:in `call'
../actionpack-2.3.4/lib/action_controller/routing/route_set.rb:437:in `call'
../actionpack-2.3.4/lib/action_controller/dispatcher.rb:87:in `dispatch'
../actionpack-2.3.4/lib/action_controller/dispatcher.rb:121:in `_call'
../actionpack-2.3.4/lib/action_controller/dispatcher.rb:130:in `build_middleware_stack'
../activerecord-2.3.4/lib/active_record/query_cache.rb:29:in `call'
../activerecord-2.3.4/lib/active_record/query_cache.rb:29:in `call'
../activerecord-2.3.4/lib/active_record/connection_adapters/abstract/query_cache.rb:34:in `cache'
..activerecord-2.3.4/lib/active_record/query_cache.rb:9:in `cache'
../activerecord-2.3.4/lib/active_record/query_cache.rb:28:in `call'
../activerecord-2.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:361:in `call'
../rack-1.0.1/lib/rack/head.rb:9:in `call'
../rack-1.0.1/lib/rack/methodoverride.rb:24:in `call'
../actionpack-2.3.4/lib/action_controller/params_parser.rb:15:in `call'
../actionpack-2.3.4/lib/action_controller/session/cookie_store.rb:93:in `call'
../actionpack-2.3.4/lib/action_controller/failsafe.rb:26:in `call'
../rack-1.0.1/lib/rack/lock.rb:11:in `call'
../rack-1.0.1/lib/rack/lock.rb:11:in `synchronize'
../rack-1.0.1/lib/rack/lock.rb:11:in `call'
../actionpack-2.3.4/lib/action_controller/dispatcher.rb:114:in `call'
../actionpack-2.3.4/lib/action_controller/reloader.rb:34:in `run'
../actionpack-2.3.4/lib/action_controller/dispatcher.rb:108:in `call'
../rails-2.3.4/lib/rails/rack/static.rb:31:in `call'
../rack-1.0.1/lib/rack/urlmap.rb:46:in `call'
../rack-1.0.1/lib/rack/urlmap.rb:40:in `each'
../rack-1.0.1/lib/rack/urlmap.rb:40:in `call'
../rails-2.3.4/lib/rails/rack/log_tailer.rb:17:in `call'
../rack-1.0.1/lib/rack/content_length.rb:13:in `call'
../rack-1.0.1/lib/rack/chunked.rb:15:in `call'
../rack-1.0.1/lib/rack/handler/mongrel.rb:64:in `process'
..... etc.
A bit lengthy, sorry ;)
Try adding
#project.address.build
to your controller. You need to instantiate the address object.
The trace doesn't lie. You're having issues with a line 27. The code you posted doesn't have 37 lines, but based on the trace I'm willing to bet it's this line. Because none others have have a '+'. If there was an implicit + its caller would've been listed in the trace.
flash[:notice] = #project.title + ' successfully created'
It's odd that it's reaching that point based on your validations. validates_presence_of should add errors on attributes that are either nil, false or "". Does the validation fail as expected when run in the console?
Here's a console friendly version of your code that could be used to track down your issue. You should be able to just paste it into the console to help track down your issue.
params = {
"project"=> {
"title"=>"", "notes"=>"","description"=>"",
"address_attributes"=>{
"city"=>"", "zip"=>"","title"=>"","country"=>"",
"suite"=>"","street"=>"","state"=>""
}
}
flash = {}
#project = Project.new(params[:project])
if #project.save
flash[:notice] = #project.title + ' successfully created'
puts "Saved. Flash: #{flash[:notice]}"
else
puts "validations failed:"
puts #project.errors.full_messages.map{|m| "\t#{m}"}.join("\n")
end

Resources