user2689778
user2689778

Reputation: 61

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.

Upvotes: 5

Views: 1300

Answers (2)

Matthias Winkelmann
Matthias Winkelmann

Reputation: 16394

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.

Upvotes: 2

Douglas G. Allen
Douglas G. Allen

Reputation: 2261

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)

Upvotes: 2

Related Questions