Search code examples
ruby-on-railsruby-on-rails-3activerecordactioncontroller

list and create on one page rails 3


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


Solution

  • 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.