向现有控制器添加方法
我对 Rails 还很陌生,希望你能帮助我理解以下内容是如何工作的。
目前我有一个名为项目(projects_controller.rb)的控制器。根据我对 ROR 的理解,每个控制器都有一些基本(固有)方法,例如索引、新建、创建、编辑、显示等。我想添加一个名为“help”的方法和一个显示帮助信息的视图。
目前一个用户可以创建多个项目。这些项目包含一组由用户填充的字段。我想添加一个用户可以访问的帮助页面(通过项目屏幕上的链接),该页面解释了每个项目字段以及如何最好地填写它。我希望这是一个独立的页面(不仅仅是项目页面上显示的信息)。
截至目前,项目的 URL 为(编辑项目时): localhost:3001/projects/id/edit
我希望帮助文件的路径为 localhost: 3001/projects/id/help
如果我希望帮助文件位于上面列出的路径中,我是否正确地假设我需要在项目控制器中创建一个名为“help”的新方法?如果是这样,我是否需要添加一些东西到routes.rb中才能使其发挥作用?我会在 Haml 中使用 link_to 函数来创建指向它的链接吗?
如果这看起来令人困惑或有很多问题,我很抱歉。我很感激你的时间。如果您对是否走在正确的道路上有任何建议,请告诉我。太感谢了!
I'm pretty new to rails, and I'm hoping you can help me understand how the following works.
At present I have a controller named projects (projects_controller.rb). From what I understand of ROR each controller has some basic (inherent) methods such as index, new, create, edit, show, etc. I would like to add a method called "help" and a view to display the help information.
At present a user can create many projects. The projects contain a set of fields that are populated by the user. I would like to add a help page that the user can access (via a link on the project screen) which explains each project field and how to best fill it out. I would like this to be an independent page (not just info displayed on the project's page).
As of now, the URL to the projects is (when editing a project): localhost:3001/projects/id/edit
I would like for the path to the help file to be localhost:3001/projects/id/help
If I want the help file to be located in the path listed above, am I correct in assuming that I need to create a new method, called "help", in the projects controller? And if so, is there something that I need to add to routes.rb to make it function? And would I use a link_to function in the Haml to create a link to it?
I'm sorry if this seems confusing or a lot of question. I appreciate your time. If you have any suggestions on whether on the right path please let me know. Thank you so much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为目前您在路线中有这个,
只需将其替换为
“并在控制器中添加方法”,并在“视图/产品”文件夹中添加名为 help.erb.html 的视图(如果您使用 erb)。
您可以使用 help_path 和 help_url
I think currently you have this in routes
just replace this with
And add method in controller and add view named help.erb.html(if you r using erb) in views/product folder.
you can use help_path and help_url