• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

[ruby]railsrender

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

载入模板文件

# 载入app/views/<controllername>/edit.html.erb
render :edit
render :action => :edit
render 'edit'
render 'edit.html.erb'
render :action => 'edit'
render :action => 'edit.html.erb'

# 载入app/views/books/edit.html.erb
render 'books/edit'
render 'books/edit.html.erb'
render :template => 'books/edit'
render :template => 'books/edit.html.erb'
render '/path/to/rails/app/views/books/edit'
render '/path/to/rails/app/views/books/edit.html.erb'
render :file => '/path/to/rails/app/views/books/edit'
render :file => '/path/to/rails/app/views/books/edit.html.erb'

String Template

render :inline => "<% products.each do |p| %><p><%= p.name %></p><% end %>"

Text Output

render :text => "OK"

JSON Output

render :json => @product

XML Output

render :xml => @product

Javasctipt Output

render :js => "alert('Hello Rails');"

 

content_type(文本头)

# text/html(default) | application/json | application/xml | application/rss
render :file => filename, :content_type => 'application/rss'

 

layout

# 使用app/views/layouts/special_layout.html.erb
# 默认使用 app/views/layouts/<controllername>.html.erb
render :layout => 'special_layout'
# no layout
render :layout => false  

Specifying Layouts for Controllers

# 定义整个controller的layout
class ProductsController < ApplicationController
  layout "inventory"
  #...
end

Choosing Layouts at Runtime

class ProductsController < ApplicationController
  layout :products_layout
 
  def show
    @product = Product.find(params[:id])
  end
 
  private
    def products_layout
      @current_user.special? ? "special" : "products"
    end
 
end

Conditional Layouts

class ProductsController < ApplicationController
  layout "product", :except => [:index, :rss]
end

 

输出Http状态

# http 状态
render :status => 500
render :status => :forbidden

 

location重定向

render :xml => photo, :location => photo_url(photo)

 

跳转

# 跳转到指定地址
redirect_to photos_url
# 后退
redirect_to :back
# 301重定向到指定地址
redirect_to photos_path, :status => 301

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Ruby的资源站点和开发工具列表发布时间:2022-07-14
下一篇:
ruby -- 问题解决(一)无法连接mysql数据库发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap