Explique qué significa (.:formato)

In ruby on rails when I run 'rake routes' the output for example is

                Prefix Verb   URI Pattern                           Controller#Action
                 users GET    /users(.:format)                      users#index
                       POST   /users(.:format)                      users#create

what does the (.:format) mean?

preguntado el 09 de septiembre de 13 a las 21:09

.json, .xml, mostly. The format of the request, what success the responds_to. -

Thank you Dave for taking the time to answer, but I have to tell you that your answer wasn't detailed enough it wasn't until I read 'ream88' answer below that I understood what you meant. -

That's why it was a comment, and not an full answer, despite it actually answering what the .:format medio. -

Ah, makes sense now. Thanks Dave! -

2 Respuestas

  • Parentheses inside a route define an optional route segment.
  • A colon followed by a name (in this case :format) defines a variable, which can be accessed later in your controller via the params picadillo.

Depending on your controller, your users can be retrieved in multiple formats in this particular example (e.g. /users.json y /users.xml). When the format variable is omitted (e.g. accessing /users), Rails defaults to the HTML format.

Be sure to check the offical Guías de rieles and especially the sections 3.1 Bound Parameters y 3.2 Dynamic Segments para obtener más información.

Respondido el 09 de Septiembre de 13 a las 22:09

Excellent information, thank you for the detail. I haven't had a chance to read through the links yet, but I will read them, so maybe they answer my next question... how do you know what the available formats are? Such as how did everyone know that .json and .xml were available formats? - Jonathan Chad Faling

Look into your controller, current Rails versions offer .json y .html by default. Again, the Rails Guides have a . about this. And be sure to check este RailsCast también. - mario uher

Thanks again! One last question since you seem really good at Rails. I'm obviously just learning here and doing it by reading a book and then trying to build a site. How did you learn rails? Did you jump right in or did you read the rails guides first? Could you kind of give a quick summary of how you learned? Then I promise I'll leave you alone :) - Jonathan Chad Faling

I learned in Ruby with a german ebook and then jumped right in into my first Rails app. This was around 2007, when the awesome RailsCasts just had started. They helped me a lot, maybe they can help you too even if some of them are already dated. Just start by #1 and proceed, the syntax may have changed since then, but the spirit of Rails is still the same :) - mario uher

Thanks for the input! Will do. - Jonathan Chad Faling

The format refers the the data format being requested. This might be JSON or XML, so your route would match:

/users.json or /users.xml

Leaving this blank, gives the HTML version.

Respondido el 09 de Septiembre de 13 a las 21:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.