简体   繁体   English

rails response_to如何工作?

[英]How exactly does rails respond_to work?

I am currently building an application that needs remote form, and it came across me that I never really understood how rails respond_to work. 目前,我正在构建一个需要远程表单的应用程序,发现我从未真正理解过rails respond_to如何工作。

For example, I have the following code in the view 例如,我在视图中有以下代码

<%= form_tag ..., :remote => true %>
<%= submit_tag "submit" %>
<% end %>

And I have a javascript partial named _home.js.erb , suppose the action related is named home . 我有一个名为_home.js.erb的javascript部分,假设相关操作名为home

I understand that if I have the following code in my controller: 我了解如果我的控制器中包含以下代码:

def home
    # ...
    respond_to do |format|
        format.html
        format.js { render :partial => 'home' }
    end
end

This will execute the javascript in _home.js.erb when the submit button for the form is pressed. 当按下表单的提交按钮时,这将在_home.js.erb执行javascript。 However, what exactly is going on when the submit button is pressed? 但是,按下提交按钮时到底发生了什么? How exactly does rails know whether to respond_to html or javascript? Rails如何确切知道是否对HTML或javascript进行respond_to

Thank you very much! 非常感谢你!

The Responder object renders content if the headers indicate that it is of type 如果标题指示其类型,则Responder对象将呈现内容

  • html HTML
  • js JS

else, a magic method is invoked that calls a object.method that is the name of the respond type (eg csv ) 否则,将调用魔术方法,该方法调用object.method作为响应类型的名称(例如csv

This requires that such a method is defined as a handler of the response type. 这就要求将这种方法定义为响应类型的处理程序。 A good discussion of how to create custom responders was answered here . 这里回答了有关如何创建自定义响应者的精彩讨论

The submit button posts the form with certain headers/mimes set. 提交按钮发布带有某些标题/ MIME设置的表单。 respond_to decided how to act based on the content type, verb and the resource status of the request. respond_to根据请求的内容类型,动词和资源状态决定如何操作。

See here for details: ActionController::Responder < Object 详情请看这里: ActionController :: Responder <Object

In the options hash the remote: true is requesting Rails to respond with javascript. 在选项哈希中, remote: true是请求Rails用javascript响应。

If you have a web inspector, you check out your network tab, submit the form, and you can see that in the http request headers that the post request is requesting javascript in the accept field. 如果您有网络检查员,则签出“网络”选项卡,提交表单,然后您可以在接受字段的http请求标头中看到发布请求正在请求javascript的信息。 If you did not have the remote tag in the options hash, your form submission would be requesting html. 如果您在options哈希中没有remote标签,则您的表单提交将请求html。

Additionally, if you do a rake routes in your project directory, you'll see that almost every route ends with the .:format keyword /posts(.:format) that allows your controller to respond to multiple types of formats a user could potentially define in the specific controller. 此外,如果在项目目录中进行rake路由,您会看到几乎所有路由都以。:format关键字/posts(.:format) ,这使您的控制器可以响应用户可能潜在的多种格式类型在特定控制器中定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM