如何为rails单页面应用程序编写单独的控制器?

时间:2012-11-14 03:13:35

标签: ruby-on-rails ruby-on-rails-3 rendering

我正在创建表单列表

Questions form  
Answers form  
Hints form 

所有这些都有不同的控制器和视图,question_controller,answers_controller,hints_controller。

现在我需要在主页的选项卡式UI中获取所有这些视图(比如home_controller,home #index)

我尝试渲染:局部,渲染:模板还有本地人,我无法实现。

可以通过将所有对象移动到同一个控制器(home_controller,但我不确定这种方法,因为它会使家庭控制器太复杂而无法管理)来轻松完成,但我需要将其保存在单独的控制器中( question_controller,answers_controller,hints_controller)并将其呈现到同一页面。我正在使用客户端验证,简单形式的宝石。

以下是我的问题控制器

class QuestionsController < ApplicationController

def index
    @question = Question.new

    @question_status = []
    @question_mode = []
    @question_type = []

    @question_lookups = Lookup.where({:lookup_for => "question"})

    @question_lookups.each do |lk|
        case lk.lookup_type
            when 'mode'
                @question_mode << lk
            when 'status'
                @question_status << lk
            else
                @question_type << lk
        end
    end

    @caa = Questioncaa.new

end

end

问题视图(使用简单表单)

<%= simple_form_for @question, :validate => true do |q| %>
<%= q.input :question_info,  :as => :ckeditor, :input_html => { :toolbar => 'Easy', :width => 750 }  %> 
<%= q.input :question_source %>
<%= q.input :is_mobile %>
<%= q.input :is_similar_question %>
<%= q.input :is_boss_question %>
<%= q.input :is_racing_question %>
<%= q.input :is_speed_question %>
<%= q.input :difficulty_level %>
<%= q.input :ideal_time %>

<%= q.input :lookups, :collection => @question_mode, :value_method => :id, :label_method => :lookup_value,:prompt => "Choose Mode", :label => :QuestionMode %>
<%= q.input :lookups, :collection => @question_status, :value_method => :id, :label_method => :lookup_value,:prompt => "Choose Status", :label => :QuestionStatus %>
<%= q.input :lookups, :collection => @question_type, :value_method => :id, :label_method => :lookup_value,:prompt => "Choose Type", :label => :QuestionType %>

<%= simple_fields_for @caa do |c| %>
    <%= c.input :needs_hints %>
    <%= c.input :needs_video_solution %>
    <%= c.input :needs_tips_tricks %>
    <%= c.input :needs_formulae %>
    <%= c.input :needs_key_concepts %>
<% end %>


<%= q.button :submit %>

<% end %>

主页视图

<div class="tab-content">
          <div class="tab-pane active" id="learning_map">

                <!-- I need to acheive this -->
                <%= render :template => "learning_map/index" %>

            </div>
            <div class="tab-pane" id="questions">

                <!-- I need to acheive this -->
                <%= render :template => "questions/index", :collection => @question_mode %>

            </div>
            <div class="tab-pane" id="answers">.

                <!-- I need to acheive this -->
                <%= render :templates => "answers/index" %>

            </div>
        </div>

请告诉我,这将非常有帮助。感谢您阅读本文。

1 个答案:

答案 0 :(得分:0)

您应该稍微更改模板的逻辑(这里是'问题'视图的示例):

1)将您的问题模板拆分为2个文件: - 带有simple_form_for表单声明的标头 - 包含<%= fields_for @question do |q| %>的_form.html.erb文件以及<%= q.input %>

等问题字段列表

2)在标题文件中添加<%= render :partial => 'form' %>

3)在您的家庭视图模板中使用<%= render :partial => 'question\form' %>

4)不要忘记在home_controller中初始化@question变量。