关于将实例变量传递给 redirect_to 方法的困惑.正如 Rails 指南中所见 [英] Confusion about passing instance variables to redirect_to method. As seen in Rails Guides

查看:44
本文介绍了关于将实例变量传递给 redirect_to 方法的困惑.正如 Rails 指南中所见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 ruby​​ on rails 指南,即 http://guides.rubyonrails.org 上的布局和渲染"主题/layouts_and_rendering.html

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

我对将实例变量传递给 redirect_to 方法感到困惑.这怎么可能?我认为 redirect_to 与重定向到另一个网页或网址有关.

I am confused about passing an instance variable to a redirect_to method. How is this possible? I thought redirect_to would be relevant for redirecting to another webpage or a url.

在指南中给出的示例中,它说明了以下内容:

In the examples given on the guide it says the following:

2.2.2 渲染动作的视图

如果要渲染对应不同动作的视图在同一个模板中,您可以使用带有名称的渲染视图:

If you want to render the view that corresponds to a different action within the same template, you can use render with the name of the view:

def update
  @book = Book.find(params[:id])
  if @book.update_attributes(params[:book])
    redirect_to(@book)
  else
    render "edit"
  end
end

渲染编辑"完全有意义,它将再次渲染那个新表单.但是 redirect_to(@book) 到底发生了什么?究竟要呈现什么以及如何将书对象重定向到?顺便说一句,书籍模型有列、名称、作者、页面等...

The render "edit" makes complete sense, its going to render that new form again. But what in the world is going on with redirect_to(@book)? What exactly is that going to render and how is a book object going to be redirected to? BTW, the book model has columns, Name, author, pages etc...

推荐答案

redirect_to 文档

redirect_to(options = {}, response_status = {}) 重定向浏览器到选项中指定的目标.记录 - URL 将通过调用生成url_for 与选项,它将引用一个命名的 URL记录.

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.

所以当 redirect_to(@book) @book 是一个带有 id 的特定记录.

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

因此,关联记录(在本例中为@book)show 方法用作模板.

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

除上述之外,如果您查看定义这些路径的 routes.rb 文件,您会注意到

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

resources :books

现在这条路线基本上被翻译为(你可以通过运行rake routes看到)

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

注意 book GET/books/:id books#show - 当你执行 redirect_to(@book)

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

这篇关于关于将实例变量传递给 redirect_to 方法的困惑.正如 Rails 指南中所见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆