我學到了一些基本的Rails,我現在正在做的一個嘗試是完全在Rails之外使用ActionController和ActionView(在非Rails應用程序中)。完全在rails外部使用Rails ActionView/Controller
到目前爲止,我有什麼是安裝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>
形式。 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法正常工作與任何問題,我可以使用大部分的有效收視ViewHelpers在ERB文件(以上Hello.html.erb使用這樣的一個亮點方法)。所以這是我可以通過使用ActionView的強大功能生成靜態html頁面的一種方式。
但我無法獲得edit_person的工作,因爲它使用form_for helper,這需要自定義url_for實現或rails標準routes.rb實現(根據錯誤消息中的提示)。我想得到這與routes.rb方法的工作,但我得到的印象是我需要Rails :: Application對象和一些url_helpers等(到目前爲止我無法得到它的工作)。有沒有辦法做到這一點?
然後,我想將這個解決方案與Webrick集成來處理來自瀏覽器的請求(當用戶創建一個人時)。
請注意,這是試圖學習Rails的一些細節,而不是解決使用標準rails框架時無法做到的任何問題的解決方案。任何幫助將不勝感激。
爲什麼你不創建一個API來訪問你的靜態網站?試圖擴展Rails的本地功能看起來非常不安全 –
感謝您的回覆。你的意思是提供靜態網頁的API嗎?我已經可以用Webrick做。我現在感興趣的是學習如何通過僅使用ActionPack和Rack(即,無需整個導軌)來提供簡單的動態頁面。我認爲Rails的一個關鍵方面是它是一個非常好的模塊化框架,用戶可以單獨嘗試並自行擴展。 – user2689778
雖然聲稱你可以在rails應用程序之外使用action_pack和action_view,但我還沒有找到一個很好的例子。你的文章是我在搜索中遇到的第一篇文章。我會問你是否找到解決辦法,然後寫下你自己的答案,這樣我們其他人就可以受益。作爲旁註:AbstractController :: Layouts不正確。請參閱https://github.com/rails/rails/blob/4-1-stable/actionview/lib/action_view.rb它現在是ActionView :: Layouts –