【问题标题】:How to get rails response object in code?如何在代码中获取rails响应对象?
【发布时间】:2011-06-22 14:24:53
【问题描述】:

我想从控制器操作响应对象,我只知道我可以得到这样的响应:

class HomeController < ApplicationController
  after_filter :generate_html

  def index

  end 

  def generate_html
    raise response.body # this will show response body content
  end  
 end

现在如何初始化控制器并获取其响应对象? 因为我想在 Rails 应用程序中编写 static_page_generator。

【问题讨论】:

    标签: html ruby-on-rails static-pages


    【解决方案1】:

    如果您只想渲染静态页面,最简单的方法就是在控制器操作中渲染页面。

     class HomeController < ApplicationController
    
       def index
         # renders index.html.haml or index.html.erb 
         # which is in the /views/static folder
         render 'static/index'
       end 
    
       def home
         # renders home.html.haml or home.html.erb 
         # which is in the /views/static folder
         render 'static/home'
       end
    
     end
    

    此外,请记住,您放入 Rails 应用程序中 /public 文件夹的任何页面都是静态的,并且可以通过 http://www.yoursite.com/index.html 访问页面来公开查看

    【讨论】:

    • 感谢您的回复。但是我想执行代码来获得响应,而不是打开浏览器查看。
    • 您的意思是生成 HTTP 响应还是生成 JSON/XML?你对你试图产生的“响应”做了什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-29
    相关资源
    最近更新 更多