Custom formats in Ruby on Rails

后端 未结 1 1966
长情又很酷
长情又很酷 2021-02-20 03:21

I\'m creating a website in Ruby on Rails, where users can login using RESTful Authentication. Someone can get a specific user using html, xml and json, just like scaffolding. Bu

1条回答
  •  孤街浪徒
    2021-02-20 04:01

    In your /config/initializers/mime_types.rb file, add a new registration for your format. It should look something like this:

    Mime::Type.register "text/x-vcard", :vcard  #The :vcard is the important part
    

    After that (you'll have to restart your app to pick up the change), you can respond to the symbol like any other format:

    # then in your controller action
    def show
      respond_to do |format|
        format.html # render html
        format.vcard { #render vcard }
      end
    end
    

    Adding from comments (thanks nanda):

    In your views folder, then, you would put the vCard template into a show.vcard.erb file (for example).

    0 讨论(0)
提交回复
热议问题