ruby on rails - How is this sent to the Show action -


i new ruby on rails , want understand how piece of code redirect show action :

def create   @article = article.new(params[:article])   @article.save    redirect_to @article end 

if define routes using rails convention of defining restful routes i.e. resources :articles in routes.rb file, redirect_to @article take show page of particular @article instance. rails doing underlying magic here.

when write resources :articles in routes.rb file, rails generating these routes automatically:

      prefix verb   uri pattern                  controller#action     articles    /articles(.:format)          articles#index              post   /articles(.:format)          articles#create  new_article    /articles/new(.:format)      articles#new edit_article    /articles/:id/edit(.:format) articles#edit      article    /articles/:id(.:format)      articles#show              patch  /articles/:id(.:format)      articles#update              put    /articles/:id(.:format)      articles#update              delete /articles/:id(.:format)      articles#destroy 

so have particular route mapped articles controller's show action:

article    /articles/:id(.:format)      articles#show 

this route matched when do: redirect_to @article , why it's taking show page of @article.

to know more how restful routes works, see this rails tutorial


Comments