Roman N
Roman N

Reputation: 5110

How to prohibit any response except html in controller

I have such code:

def show
  @attachment = Attachment.find(params[:id])

  respond_to do |format|
    format.html
  end
end

This code allows respond on json, but renders nothing. How to show custom page on any other request except html?

Upvotes: 1

Views: 89

Answers (3)

boulder
boulder

Reputation: 3266

You can restrict the format of your routes when you define them in your config/routes.rb

scope :format => true, :constraints => {:format => :html} do
  resources :attachments
end

Define all the routes whose format you want to restrict within that scope.

Upvotes: 2

jdoe
jdoe

Reputation: 15771

Try:

before_filter do
  render text: 'Wrong type', status: 406 unless request.format == Mime::HTML
end

Upvotes: 2

Vasiliy Ermolovich
Vasiliy Ermolovich

Reputation: 24617

you can use format.any for any other request except html:

def show
  @attachment = Attachment.find(params[:id])

  respond_to do |format|
    format.html { render text: => 'This is html' }
    format.any  { render :text => "Only html is supported" }
  end
end

Upvotes: 2

Related Questions