Using Rails ActionView/Controller completely outside rails - ruby-on-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.

Related

RSS Feed Entries - Encoding::CompatibilityError incompatible character encodings: ASCII-8BIT and UTF-8

I am currently working on an RSS feed that was working for a while but now has an encoding issue. After trying many of the solutions here unsuccessfully, I have deduced that I may be having issues with the feed parser.
The error starts with the index page:
ActionView::Template::Error (incompatible character encodings: ASCII-8BIT and UTF-8):
11:
12: <div class="entry_wrapper">
13: <div class="entry_box">
14: <% feed.entries.each do |entry| %>
15: <p class="entry_title"><%= sanitize link_to entry.title, entry.url %></p>
16:
17:
app/models/feed.rb:6:in `entries'
app/views/feeds/index.html.erb:14:in `block in _app_views_feeds_index_html_erb__2672739530113604393_70126099705280'
app/views/feeds/index.html.erb:5:in `_app_views_feeds_index_html_erb__2672739530113604393_70126099705280'
Here is larger section of the Feed#Index page:
<% #feeds.each do |feed| %> # Line 5 here
<p class="feed_url">
<%= link_to feed.url, feed %>
<%= link_to "Edit", edit_feed_path(feed), class: "blue" %>
<%= link_to "Delete", feed_path(feed), method: :delete, data: { confirm: 'Are you sure you want to delete this feed url?' }, class: "blue" %>
</p>
<div class="entry_wrapper">
<div class="entry_box">
<% feed.entries.each do |entry| %>
<p class="entry_title"><%= sanitize link_to entry.title, entry.url %></p>
I deleted the Feed URL's in the console so the page rendered fine without any urls to pull from. However, as soon as I added one, I got the same error as before.
I tried testing the encoding of the Feed entries in the console and got the following error:
2.1.1 :001 > g = Feed.last
Feed Load (0.1ms) SELECT "feeds".* FROM "feeds" ORDER BY "feeds"."id" DESC LIMIT 1
=> #<Feed id: 9, name: nil, created_at: "2016-01-18 05:01:54", updated_at: "2016-01-18 05:01:54", url: "http://feeds.feedburner.com/MattsTravelSite">
2.1.1 :002 > g.entries
Encoding::CompatibilityError: incompatible character encodings: ASCII-8BIT and UTF-8
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/entities.rb:77:in `gsub'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/entities.rb:77:in `decode'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/entity_decoder.rb:14:in `decode'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/entity_decoder.rb:5:in `try_decode'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/sax_parser.rb:151:in `on_text'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/parser.rb:541:in `_rule_33'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/parser.rb:239:in `block in each_token'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/lexer.rb:237:in `call'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/lexer.rb:237:in `add_token'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/lexer.rb:439:in `on_element_end'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/lexer.rb:190:in `advance_native'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/lexer.rb:190:in `block in advance'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/lexer.rb:137:in `read_data'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/lexer.rb:189:in `advance'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/parser.rb:236:in `each_token'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/parser.rb:269:in `parse'
... 20 levels...
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:241:in `load'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:241:in `block in load'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:232:in `load_dependency'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:241:in `load'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/spring-1.3.6/lib/spring/commands/rails.rb:6:in `call'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/spring-1.3.6/lib/spring/command_wrapper.rb:38:in `call'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/spring-1.3.6/lib/spring/application.rb:183:in `block in serve'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/spring-1.3.6/lib/spring/application.rb:156:in `fork'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/spring-1.3.6/lib/spring/application.rb:156:in `serve'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/spring-1.3.6/lib/spring/application.rb:131:in `block in run'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/spring-1.3.6/lib/spring/application.rb:125:in `loop'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/spring-1.3.6/lib/spring/application.rb:125:in `run'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/spring-1.3.6/lib/spring/application/boot.rb:18:in `<top (required)>'
from /Users/danieluribe/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/danieluribe/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
I double-checked to see if the website I am pulling from is in UTF-8 encoding at the W3C-Validator website:
This document was successfully checked as well-formed XML!
Result: Passed, 1 warning(s)
Address:
http://feeds.feedburner.com/MattsTravelSite
Encoding: utf-8
Doctype: XML
Root Element: feed
Root Namespace: http://www.w3.org/2005/Atom
I tried "forcing" the encoding in the model, but I think my coding skills are a little lacking in terms of creating methods. Here is the entries model I created originally for the Feed that is being used in the Feed#Index.
class Feed < ActiveRecord::Base
has_many :items
def entries(num = 3)
Feedjira::Feed.add_common_feed_entry_element("img")
feed = Feedjira::Feed.fetch_and_parse(url)
feed.entries.take(num)
# add_items(feed.entries) # Saving items to database
end
....
Since this model was working before, I'm not sure what happened. I did take a few months from the app so I don't know if anything changed with Feedjirra. Any help would be greatly appreciated.

Rails wrong number of arguments (1 for 2) in controller

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

Missing Partial error in view spec generated by scaffold in rails 3.1.10

I just generated a scaffold in rails 3.1.10 and when I run rspec I get the following error:
Failures: 1) organizations/edit.html.erb renders the edit organization form
Failure/Error: render
Missing partial /form with {:handlers=>[:erb, :builder, :coffee], :formats=>[:html], :locale=>[:en, :en]}. Searched in:
* "/home/ubuntu/Documents/github/LocalSupport/app/views"
# ./app/views/organizations/edit.html.erb:3:in `_app_views_organizations_edit_html_erb__643434798_103899540'
# ./spec/views/organizations/edit.html.erb_spec.rb:18:in `block (2 levels) in <top (required)>'
2) organizations/new.html.erb renders new organization form
Failure/Error: render
Missing partial /form with {:handlers=>[:erb, :builder, :coffee], :formats=>[:html], :locale=>[:en, :en]}. Searched in:
* "/home/ubuntu/Documents/github/LocalSupport/app/views"
# ./app/views/organizations/new.html.erb:3:in `_app_views_organizations_new_html_erb__277486420_91000650'
# ./spec/views/organizations/new.html.erb_spec.rb:17:in `block (2 levels) in <top (required)>'
Finished in 0.68276 seconds 29 examples, 2 failures, 2 pending
It seems that the spec can't find the form partial when it runs render. Here's the failing part in the rails generated spec (it fails when it reaches the "render" keyword):
require 'spec_helper'
describe "organizations/index.html.erb" do
before(:each) do
assign(:organizations, [
stub_model(Organization,
:name => "Name",
:address => "Address",
:postcode => "Postcode",
:email => "Email",
:description => "Description",
:website => "",
:telephone => "Telephone"
),
stub_model(Organization,
:name => "Name",
:address => "Address",
:postcode => "Postcode",
:email => "Email",
:description => "Description",
:website => "",
:telephone => "Telephone"
)
])
end
it "renders a list of organizations" do
render
# Run the generator again with the --webrat-matchers flag if you want to use webrat matchers
assert_select "tr>td", :text => "Name".to_s, :count => 2
I've searched around this on google but can't find anything specific to this scaffold. I've found this SO post:
Rails: "missing partial" when calling 'render' in RSpec test
but it seems to suggest that render is the right way to go, i.e. the scaffold is correct. Any ideas?
Many thanks in advance
I have found at least a temporary solution which is to add the following to the spec before the render operation:
view.lookup_context.prefixes = %w[organizations application]
based on this documented rails issue here:
https://github.com/rails/rails/issues/5213
The file "./app/views/organizations/edit.html" has a call at line 3 which probably reads something like
render "form" which can equate to => "organizations/_form.html.erb"
this file is missing....
you can either provide one or edit the line out in edit.html(.erb/.haml)

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