完全在rails之外使用Rails ActionView / Controller

时间:2014-01-20 15:34:58

标签: ruby-on-rails ruby-on-rails-4

我学到了一些基本的Rails,我现在尝试的其中一个尝试是在Rails之外完全使用ActionController和ActionView(在非rails应用程序中)。

到目前为止,我所拥有的是安装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(要放在Views文件夹中)

<html>
  <body>
    <p>
      <%= highlight( 'This is Hello world', 'Hello world') %>
    </p>
  </body>
</html>

Form.html.erb(将被放置在Views文件夹中)

<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>

以下是上述程序的输出

   <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>'

say_hello方法可以解决任何问题,我可以在erb文件中使用大多数ActiveView ViewHelpers(上面的Hello.html.erb使用了一个这样的突出显示方法)。因此,我可以通过使用ActionView的强大功能生成静态html页面。

但是我无法使edit_person工作,因为它使用form_for帮助器,这需要自定义url_for实现或rails标准routes.rb实现(如错误消息中所示)。我想使用routes.rb方法,但我得到的印象是我需要Rails :: Application对象和一些url_helpers等(到目前为止,我无法使它工作)。有没有办法做到这一点?

之后我想将此解决方案与Webrick集成以处理来自浏览器的请求(当用户创建一个人时)。

请注意,这是尝试了解Rails的一些细节,而不是解决使用标准rails框架无法解决的任何问题。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

不是答案,只是针对某些相关代码。 为了简化一点,我知道它仍然是错误,但你会在错误中得到一些数据。 我将在代码后显示其中的一部分。

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

暂时跳过视图处理程序。

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)

顶部应该足够好,因为它无论如何只是树木。

现在使用类视图运行。

我们得到了类似的结果:     OUT:

  

ruby​​ sample.rb       “\ n \ n

\ n这是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)

答案 1 :(得分:1)

Rails 5引入了ActionController::Renderer,这使得在Rails之外使用Rails模板系统更方便一些。

示例:

 renderer = ApplicationController.renderer
 content  = renderer.render template: 'ruby_template'

(来自the Tests

这有点有用,因为普通再培训局受到严重限制。我在发电机中使用它,没有局部的生活会很不方便。

相关问题