Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Thananjaya Chakravarthy
Thananjaya Chakravarthy
4,672 Points

Error Throwing, 'unknown format', pointing towards respond_to |format|. Any solutions for this?

Rails version 5, throwing error in the sense unknown format respond_to |format| do format.js end Is there any solutions for it

Thananjaya Chakravarthy
Thananjaya Chakravarthy
4,672 Points

code is

respond_to do |format| format.js end

1 Answer

Ari Misha
Ari Misha
19,323 Points

Hiya there! The original syntax for respond_to is respond_to(*mimes). Say client wants response in HTML or JS or even XML, you could just say something like this:

def index
  @people = Person.all
  respond_to do |format|
    format.html
    format.js
    format.xml { render xml: @people }
  end
end

By default your application respond with every format. But for simple whitelisting of format in your application , i'd rather use this:

def index
  @people = Person.all
  respond_to :html :js
end

Rails is smart enough to know what kinda format is asked of in request hash and it'll respond with appropriate format , even if its a simple API application layer. Now regarding your code, unfortunately , your codes not enough for me to analyze and gimme my feedback. Thats why i provided you a nice example of how to use respond_to method. I hope it solved your issue and if not please do publish your full controller code. Good luck!

~ Ari