在一页上列出并创建 Rails 3

发布于 2024-11-19 11:54:30 字数 279 浏览 3 评论 0原文

我想制作一个仪表板页面,顶部有一个创建操作(表单),下面有一个列表操作...(显示所有已提出的问题)我想知道执行此操作的最佳方法是什么。

我有一个带有列表和创建操作的 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 技术交流群。

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

发布评论

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

评论(2

染柒℉ 2024-11-26 11:54:30

您应该创建 QuestionsController,并将表单部分包含在索引视图中。该表单应该简单地遵循创建操作,并且该操作应该在错误时(记住可能存在验证错误)呈现索引操作,并且在成功后应该重定向回索引。

除非你还需要从其他地方创建问题,否则就更复杂了。

更新

您也可以创建 DashboardsController,但我只会用它来显示仪表板(显示操作和单一资源,而不是在 paths.rb 中的资源),然后通常按照新问题形式进行 QuestoinsController#create ,然后重定向回 DashboardsController#show。这样,如果您还在仪表板上显示多种资源类型,则更加 RESTful,因为您显示单一仪表板(包含问题和其他资源),但您可以按照 QuestionsController#create 来创建问题。

更新2

如果您(或任何其他人需要),要将其放在一个地方:

  1. 在您的routes.rb文件中定义资源:

    资源:问题
    
  2. 在您的 QuestionController 中:

    类 QuestionsController <应用控制器
      定义索引
        设置问题
      结尾
    
      定义创建
        @question = Question.new(params[:question])
        如果@question.save
          重定向 questions_path, :notice => “问题创建成功。”
        别的
          设置问题
          渲染:动作=> :指数
        结尾
      结尾
    
      私人的
    
      def 设置问题
        @questions = Question.order(:name).page(params[:page])
        # 或任何其他方法来获取您的所有问题
    
        @question ||= Question.new
      结尾
    结尾
    
  3. 在您的 app/views/questions/index.html.erb 视图中:

    <% @questions.each do |question| %>
      <%# 将问题显示为表行 %>
    <%结束%>
    
    <%渲染:模板=> “形式”,:当地人=> {:问题=> @问题} %>
    
  4. app/views/questions/_form.html.erb 中code> 您只需定义新问题的标准形式:

    <%= form_for Question do |f| %>
      <%# 表单字段 %>
    <%结束%>
    

那么您不需要 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):

  1. In your routes.rb file define resources:

    resources :questions
    
  2. In your QuestionController:

    class QuestionsController < ApplicationController
      def index
        setup_questions
      end
    
      def create
        @question = Question.new(params[:question])
        if @question.save
          redirect questions_path, :notice => "Successfully created question."
        else
          setup_questions
          render :action => :index
        end
      end
    
      private
    
      def setup_questions
        @questions = Question.order(:name).page(params[:page])
        # or any other method to fetch all your questions
    
        @question ||= Question.new
      end
    end
    
  3. In your app/views/questions/index.html.erb view:

    <% @questions.each do |question| %>
      <%# display question as table row %>
    <% end %>
    
    <% render :template => "form", :locals => {:question => @question} %>
    
  4. in app/views/questions/_form.html.erb you just define your standard form for new question:

    <%= form_for question do |f| %>
      <%# form fields %>
    <% end %>
    

Then you don't need view for new action, as this action would just display form for new question, and you have this form in index action.

烟火散人牵绊 2024-11-26 11:54:30

您可以在例如索引页面上将这两者结合起来。使用 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.

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