Hobo 1.3 Render :json creating content-type="text/html" instead of "application/json"

211 views
Skip to first unread message

Paul

unread,
Mar 13, 2012, 8:38:44 PM3/13/12
to hobo...@googlegroups.com
Hi,

I'm rendering a json response from my controller simply as: 

    hobo_show do 
      @this = find_instance
      render :json => @this.drill_detail
    end 

Problem is the response posts as content-type="text/html".

Rails 3 doc says it should render as "application/json".

I've tried changing the render line to:
      render :json => @this.drill_detail, :content_type => 'application/json'

But, still "text/html".

How do I get content-type='application/json'

Thank you in advance! I really appreciate all the posts you all do that keep me moving well with Hobo!

-Paul

I'm currently using hobo 1.3.0-pre31

Matt Jones

unread,
Mar 13, 2012, 8:52:26 PM3/13/12
to hobo...@googlegroups.com

On Mar 13, 2012, at 8:38 PM, Paul wrote:

> Hi,
>
> I'm rendering a json response from my controller simply as:
>
> hobo_show do
> @this = find_instance
> render :json => @this.drill_detail
> end
>
> Problem is the response posts as content-type="text/html".
>
> Rails 3 doc says it should render as "application/json".
>

The block passed to hobo_show is called in a structure that looks (roughly) like this:

def hobo_show
...find the record, etc...
if block_given?
respond_to do |format|
yield format
end
else
format.html { yield }
end
end

If you want to do what you're describing, you'll need to add the format argument to your block. I don't believe you have to *use* it, though - so you may be able to skip adding a 'format.json' block.

--Matt Jones

Paul Hanson

unread,
Mar 14, 2012, 8:28:37 PM3/14/12
to hobo...@googlegroups.com
Thank you Matt. So nice to be able to dialog!

I changed my controller code to:

  def drill_detail_json 
    hobo_show do |format|
      @this = find_instance
      render :json => @this.drill_detail
    end 
  end 

and also tried:

  def drill_detail_json 
    hobo_show do |format|
      @this = find_instance
      format.json {render :json => @this.drill_detail}
    end 
  end 

Both of the above resulted in just a blank page being rendered.

I then was able to confirm that:

  def drill_detail_json 
    hobo_show do
      @this = find_instance
      render :json => @this.drill_detail, :content_type => 'application/json'
    end 
  end 

gave me the results I wanted with content-type="application/json".

Am I not understanding your instructions? I'm happy to be getting the results I want, but I'm anxious to understand the best method. Learning the correct methods will be a real benefit I'm sure.

Thanks again,
 Paul


--
You received this message because you are subscribed to the Google Groups "Hobo Users" group.
To post to this group, send email to hobo...@googlegroups.com.
To unsubscribe from this group, send email to hobousers+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/hobousers?hl=en.




--
Paul Hanson
PaulRHa...@gmail.com

Reply all
Reply to author
Forward
0 new messages