简体   繁体   English

Rails中的辅助方法

[英]Helper method in rails

I am currently learning RoR and I have problem with understanding helper-method's. 我目前正在学习RoR,但在理解辅助方法时遇到问题。 This example is from ruby guides. 这个例子来自红宝石指南。

So, when I click submit form in this example I call create method in controller. 因此,在此示例中单击提交表单时,我在控制器中调用create方法。 But, when I click "back" button I go to index action. 但是,当我单击“后退”按钮时,我进入index动作。

<%= form_for :post, url: posts_path do |f| %>
  <p>
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </p>

  <p>
    <%= f.label :text %><br>
    <%= f.text_area :text %>
  </p>

  <p>
    <%= f.submit %> 
  </p>
<% end %>

<%= link_to "Back", posts_path %>

Why with the same url posts_path (this is a helper method, isn't it ?) I have different results? 为什么使用相同的url posts_path (这是一个辅助方法,不是吗?),我得到的结果不同?

HTTP method is different in your case HTTP方法因您而异

method: POST, posts_path -> create action
method: GET, posts_path -> index action

Looking at the result of rake routes | grep post rake routes | grep post的结果rake routes | grep post rake routes | grep post will give you some idea rake routes | grep post给你一些想法

重定向使用GET,而表单使用POST。

The difference is that the form submits a POST request to posts_path while the back button a GET . 不同之处在于,表单向posts_path提交POST请求,而向后按钮GET This is the common REST way Rails handles its resources. 这是Rails处理资源的常用REST方法。 Have a look at the rails docs for more info on that 看看rails文档以获取更多信息

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

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