向现有控制器添加方法

发布于 2024-11-29 09:03:12 字数 646 浏览 3 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪荡不羁 2024-12-06 09:03:12

我认为目前您在路线中有这个,

resources :products do  

end

只需将其替换为

resources :products do  
  get :help, :on => :member, :as => :help  
end

“并在控制器中添加方法”,并在“视图/产品”文件夹中添加名为 help.erb.html 的视图(如果您使用 erb)。

您可以使用 help_path 和 help_url

I think currently you have this in routes

resources :products do  

end

just replace this with

resources :products do  
  get :help, :on => :member, :as => :help  
end

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文