4

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

Thijs
  • 387
  • 5
  • 20

2 Answers2

3

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.

MBO
  • 30,379
  • 5
  • 50
  • 52
  • okay, now i've put the index action in the new form there i say to render the index template, but then it gets the error that it's missing the @questions = Question.all so then i have to add this to the new action in the controller... but isn't there a better way, i am repeating myself because it's there allready in the index action... – Thijs Jul 12 '11 at 09:22
  • That's right, you need to setup all data needed for index view in both actions (in create when you render :index). You could define method in controller and call this method from both actions if you have more data to setup, I would do it this way probably. – MBO Jul 12 '11 at 09:38
  • Okay, then this is probably the best way...? Or maybe i can do a form for and define the controller and it's action... <% form_for @question, :url => {:controller => :question, :action => :create } %> When i call the _form partial... – Thijs Jul 12 '11 at 12:29
  • If you have newly instantiated question (`@question = Question.new` in controller), then you only need `form_for @question do...`, you don't need to specify `:url`. Of course if you have `resources :question` in your routes.rb file to define named routes for questions – MBO Jul 12 '11 at 13:03
  • Wow thanks, i'm gonna try it! A little question: Is :page something from another model? @questions = Question.order(:name).page(params[:page]) Again thank you so much for taking the time!! – Thijs Jul 13 '11 at 09:35
  • That was just example. I use "kaminari" gem for pagination in my current project and it adds this method for selection which part of records to return. You just replace this line with how you retrieve your collection from database, with pagination if you have, or other search criteria – MBO Jul 13 '11 at 10:23
  • Ah okay! Thanks for the kaminari tip, i've just watched the railscast on it #254 Look nice! – Thijs Jul 13 '11 at 11:44
1

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.

Sergey Kishenin
  • 5,099
  • 3
  • 30
  • 50