简体   繁体   English

Rails通过Post获取Model:id

[英]Rails get Model :id via Post

I am looking for a way to access a model via its :id. 我正在寻找一种通过其:id访问模型的方法。

My project looks like this: 我的项目如下所示:

First someone can register himself in a form. 首先,有人可以用表格注册自己。 Then he gets forwarded to a page where he can edit the things he entered. 然后,他进入页面,可以在其中编辑输入的内容。 Now I do not want that you can see something in the URL. 现在,我不希望您可以在URL中看到某些内容。

Edit: 编辑:

I was maybe a little unclear: 我可能还不清楚:

There is a form, where you can enter some things. 有一个表格,您可以在其中输入一些内容。 After you submitted those things, you will be forwarded to another page with an URL like 'www.example.com/entry'. 提交这些内容后,您将被转到具有“ www.example.com/entry”之类的URL的另一个页面。 There I want to show what the person entered. 我想在那里显示该人输入的内容。 And I do not want an URL like 'www.example.com/entry?id=12' 而且我不希望使用类似“ www.example.com/entry?id=12”的网址

I hope that clarified some things 我希望这可以澄清一些事情

Your answer is a bit lacking in details, so I'll do my best here. 您的答案有些细节,因此,我将尽力而为。 Essentially, if you do not want to display the url parameters, then you will have to use the post HTTP method to submit your forms (which you should be doing anyway). 本质上,如果您不想显示url参数,则必须使用post HTTP方法提交表单(无论如何,您都应该这样做)。

In your routes.rb file, you'll need to define your route to look something like this: 在您的routes.rb文件中,您需要定义路由,如下所示:

post 'route', to: 'controller#action'

Data submitted via the post method is typically submitted via a form. 通过post方法提交的数据通常是通过表单提交的。 I would recommend using rails conventions like: 我建议使用Rails约定,例如:

  • the rails form_for helper --> more details rails form_for助手-> 更多详细信息
  • resources since they typically give you the routes you want. resources因为它们通常会为您提供所需的路线。 To modify your routes beyond the defaults, I'd advise looking at the rails routing guide . 要修改默认设置以外的路线,建议您参考Rails的布线指南

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

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