Confusion about passing instance variables to redirect_to method. As seen in Rails Guides

前端 未结 2 1928
猫巷女王i
猫巷女王i 2020-12-05 06:10

I am studying the ruby on rails guides namely, the \"layouts and rendering\" topic at http://guides.rubyonrails.org/layouts_and_rendering.html

I am confused about pa

相关标签:
2条回答
  • 2020-12-05 06:43

    redirect_to documentation

    redirect_to(options = {}, response_status = {}) Redirects the browser to the target specified in options. Record - The URL will be generated by calling url_for with the options, which will reference a named URL for that record.

    So when one does redirect_to(@book) @book is a specific record with an id .

    Thus, the associated records (in this case @book) show method is used as a template.

    In addition to above, if you look at the routes.rb file which defines these paths you will notice

    resources :books
    

    Now this route is essentially translated as (you can see by running rake routes)

        books GET    /books(.:format)                   books#index
              POST   /books(.:format)                   books#create
     new_book GET    /books/new(.:format)               books#new
    edit_book GET    /books/:id/edit(.:format)          books#edit
         book GET    /books/:id(.:format)               books#show
              PUT    /books/:id(.:format)               books#update
              DELETE /books/:id(.:format)               books#destroy
    

    Notice the book GET /books/:id books#show - which gets matched when you do redirect_to(@book)

    0 讨论(0)
  • 2020-12-05 06:44

    It'll redirect to a book, for example "/books/65"

    0 讨论(0)
提交回复
热议问题