新手问题 Rails controller 里面 format 都有什么类型?

i5ting · 2013年02月25日 · 最后由 i5ting 回复于 2013年02月25日 · 3322 次阅读

目前我所知 format 有 4 种

  • js
  • html
  • xml
  • json

代码如下


respond_to do |format|
    format.js { render :layout => false }
    format.html # show.html.erb
    format.json { render json: @project }
    format.xml { render :xml => @people.to_xml }
 end

还有其他的类型么?怎么查看文档呢?

这个类型你可以随便先啊

format.fuck { render json: @project }

这样也可以啊,只要你的 url 里带上:format=>:fuck 就会返回后面的 json 的那个@project

这样讲应该明白了吧?

看看Mime::Type

#2 楼 @diudiutang 谢谢啊,看不出什么内容啊,我之前确实搜过的

#3 楼 @tumayun 谢谢啊,非常好

Class Mime::Type < Object actionpack/lib/action_dispatch/http/mime_type.rb Encapsulates the notion of a mime type. Can be used at render time, for example, with:

class PostsController < ActionController::Base
  def show
    @post = Post.find(params[:id])

    respond_to do |format|
      format.html
      format.ics { render :text => post.to_ics, :mime_type => Mime::Type["text/calendar"]  }
      format.xml { render :xml => @people.to_xml }
    end
  end
end
需要 登录 后方可回复, 如果你还没有账号请 注册新账号