PUT down proper TITLE#
config\router.rb#
Meaning of (.:format)#
(.:format) refers to a variable/parameter you can pass to the helper that will turn the request into the format defined:
/posts.json # will handle the requests as JSON
/posts # will handle the request as the default, html.
The parenthesis denote an optional bound parameter in the pattern. It's saying that if there is a dot in the URL followed by some text, that will become available as params[:format], but that is optional for the route to be matched.
That means that the URL can pass in the format:
GET /posts
GET /posts.json
GET /posts.pdf
GET /posts.xml
The parenthesis denote an optional bound parameter in the pattern. It's saying that if there is a dot in the URL followed by some text, that will become available as params[:format], but that is optional for the route to be matched.
When building a URL with a _path or _url helper, you can pass the format as another value for the URL:
# href will be /posts.json
link_to 'Posts JSON', posts_path(:json)
# href will be /posts/1234.json
link_to @post.title, post_path(@post, :json)
In the controller, you can respond to these formats in different ways:
def index
@posts = Post.order(created_at: :desc)
respond_to do |format|
format.html # This will render `index.html.erb` automatically
format.json { render json: @posts }
format.csv do
# Do some CSV stuff here
end
end
end
since :action and :id are optional parameters, denoted by parentheses.
This is referring to the example route:
get ':controller(/:action(/:id))'