Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

responder.rb 'text' type not working as expected

#3966

I am trying to understand the Rails 3 responder functionality and respond_with. However, for the built-in 'text' mime-type, it seems a bit counter-intuitive. responder.rb seems to only check for 'to_text' in my model -- it is never called and I have to overwrite 'to_s' to get the expected results.

controller

respond_to :html, :text, :only => [:show, :random]

def show
  @quote = Quote.find(params[:id])
  respond_with(@quote)
end
  
def random
  @quote = Quote.random
  respond_with(@quote) do |format|
    format.html { render :action => :show }
  end
end

model

this will display something like <Quote:0x3692f7c>

def to_text
  "blah" # no matter what's in here
end

changed model to this and works as expected

def to_text # if I remove to_text, I get "Missing template quotes/show with {:formats=>[:text]} in view path"
end

def to_s
  "#{body} --#{owner}"
end

Reported by mculp · February 15th, 2010 @ 04:03 PM

State: resolved
Milestone: 3.0.2
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. José Valim
  2. José Valim
    José Valim
    • State changed from new to resolved

    February 16th, 2010 @ 10:25 PM

  3. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to Low

    October 15th, 2010 @ 11:01 PM