在一页上列出并创建 Rails 3
我想制作一个仪表板页面,顶部有一个创建操作(表单),下面有一个列表操作...(显示所有已提出的问题)我想知道执行此操作的最佳方法是什么。
我有一个带有列表和创建操作的 QuestionsController。所以我确实需要使用 createlist 操作创建一个 DashboardController...?并从 QuestionController 渲染两个模板...?
或者我是否需要创建一个带有列表的 DashboardController 并在它们上引用问题模型创建操作?
此致, 泰斯
I want to make a dashboard page with on top a create action (form) and underneath it a list action... (to display all the questions that have been made) I was wondering what is the best way to do this.
I have a QuestionsController with a list and a create action. So i do need to make a DashboardController with a createlist action...? And there render the two templates from the QuestionController...?
Or do i need to make a DashboardController with both the list and create actions on them refering to the Questions model?
Best regards,
Thijs
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该创建 QuestionsController,并将表单部分包含在索引视图中。该表单应该简单地遵循创建操作,并且该操作应该在错误时(记住可能存在验证错误)呈现索引操作,并且在成功后应该重定向回索引。
除非你还需要从其他地方创建问题,否则就更复杂了。
更新:
您也可以创建 DashboardsController,但我只会用它来显示仪表板(显示操作和单一资源,而不是在 paths.rb 中的资源),然后通常按照新问题形式进行 QuestoinsController#create ,然后重定向回 DashboardsController#show。这样,如果您还在仪表板上显示多种资源类型,则更加 RESTful,因为您显示单一仪表板(包含问题和其他资源),但您可以按照 QuestionsController#create 来创建问题。
更新2:
如果您(或任何其他人需要),要将其放在一个地方:
在您的
routes.rb
文件中定义资源:在您的
QuestionController
中:在您的
app/views/questions/index.html.erb
视图中:在
app/views/questions/_form.html.erb
中code> 您只需定义新问题的标准形式:那么您不需要
new< 的视图/code> 操作,因为此操作只会显示新问题的表单,并且您在
index
操作中有此表单。You should create QuestionsController, with form partial included on index view. This form should simply follow to create action, and this action should at error (remember that there could be validation errors) render index action, and after success it should redirect back to index.
Unless you also need to create questions from other places, then it's more complicated.
UPDATE:
You could also create DashboardsController, but I would only use it to display dashboard (show action and singular resource, not resources in routes.rb), and then normally follow new question form to QuestoinsController#create, and redirect back to DashboardsController#show. This way it's more RESTful if you also show more than one resource type on dashboard, because you show singular dashboard (with questions and other resources), but you follow to QuestionsController#create to create Question.
UPDATE 2:
To make it in one place if you (or anyone else needs):
In your
routes.rb
file define resources:In your
QuestionController
:In your
app/views/questions/index.html.erb
view:in
app/views/questions/_form.html.erb
you just define your standard form for new question:Then you don't need view for
new
action, as this action would just display form for new question, and you have this form inindex
action.您可以在例如索引页面上将这两者结合起来。使用
form_for(@question)
块构建表单并显示其下的@questions
集合。确保您应该在控制器的索引操作中定义@question
和@questions
。You can combine both of this at, for ex., index page. Biuld form with
form_for(@question)
block and display collection of@questions
uder it. For sure you should define@question
and@questions
in index action of controller.