Rails 不渲染部分(_new_multiple_choice.rjs)

发布于 2024-10-30 11:41:33 字数 7803 浏览 4 评论 0原文

#online_exam_controller
class OnlineExamController < ApplicationController
   def index
    @user=current_user
    @employee=Employee.find_by_employee_number(@user.username)
    # flash[:notice]="#{@employee.id}"
    @subjects=EmployeesSubject.find_all_by_employee_id(@employee.id)
    @subject_name=[]
    @batch_details=[]
    @display_details=[[]]
    @count=0
    for @t in @subjects
     @subject_name[@count]=Subject.find(@t.subject_id,:select=>'name,code,batch_id')
     @batch_details[@count]=Batch.find(@subject_name[@count].batch_id,:select =>'name')
     @display_details[@count][0]=@subject_name[@count].name+" "+@batch_details[@count].name
     @display_details[@count][1][email protected]_id
     @count+=1
    end
   end
   def show
    if params[:subject_id]==''
     @question_type=[]
    else
     @question_type=['multiple_choice','fill_ups','matches','descriptive']
     @subject_id=params[:subject_id]

   end
   respond_to do |format|
   format.js {render:action=>'show' }
   end
  end
  def new_multiple_choice
   @quiz=MultipleChoiceQuestion.new
   @subject=Subject.find params[:id] if request.xhr? and params[:id]
   respond_to do |format|
    format.js {render :action => 'new_multiple_choice'}
   end
  end
 end
#index.html.erb
<div id="content-header">
<img src="/images/show_settings.png" alt="Subjects"/>
<h1>Subjects</h1>
<h3>Home</h3>
 <div id="app-back-button">
 <%= link_to_function image_tag("/images/buttons/back.png",:border => 0), "history.back()" %>
 </div>
 </div>

 <div id="page-yield">
 <% unless flash[:notice].nil? %>
 <p class="flash-msg"> <%= flash[:notice] %> </p>
 <% end %>
 <div class="box">

  <div class="label-field-pair">
  <label for="student_course">Select a batch:
  <%= image_tag("loader.gif",:align => "absmiddle",:border => 0,:id => "loader", :style =>"display: none;" ) %>
  </label>
  <div class="text-input-bg">
  <%= select :subject,:id,
   @display_details.map {|b| [b[0],b[1]]},
  {:prompt => 'selecct subject'},
  {:onchange =>"#{remote_function(
  :url=>{:action => 'show'},
  :before=> "Element.show('loader')",
  :success => "Element.hide('loader')" )}"}%>

  </div></div>

  <div id="subjects"></div>
  <div id="modal-box" style="display:none;"></div>

  <div class="extender"></div>
  </div></div>
  #show.rjs
  if @question_type.empty?
   page.replace_html 'subjects', :partial => 'question_bank'
  else
   page.replace_html 'subjects', :partial => 'question_bank'
  end
  #_question_bank.html.erb
  <% unless @question_type.nil? %>
   <div class="add_sub">
   <%= link_to_remote 'Add Multiple Question ', :url => { :action =>   'new_multiple_choice', :id => @subject_id } %>
   </div>
   <div class="add_sub">
    <%= link_to_remote 'Add Fill Up Question', :url => { :action => 'new_fill_ups', :id => @subject_id } %>
   </div>
   <div class="add_sub">
   <%= link_to_remote 'Add Match Question', :url => { :action => 'new_matches', :id => @subject_id } %>
   </div>
   <div class="add_sub">
   <%= link_to_remote 'Add Descriptive Question', :url => { :action => 'new_descriptive', :id => @subject_id } %>
   </div>
   <% end %>
  #new_multiple_choice.rjs
  page.replace_html 'modal-box', :partial => 'new_multiple_choice'
  page << "Modalbox.show($('modal-box'), {title: 'Add new multiple choice questions', width: 500});"
   #_new_multiple_choice.html.erb

   <div id="grading-levels-form">
   <h4><%= @subject.name unless @subject.nil? %></h4>

   <% form_remote_for @quiz do |f| %>

    <% subject_id = (@subject.nil? ? nil : @subject.id) %>
    <% batch_id = (@subject.nil? ? nil : @subject.batch_id ) %>
    <%= f.hidden_field :batch_id, :value => batch_id %>
    <%= f.hidden_field :subject_id, :value => elective_group_id %>

     <div id="form-errors"></div>

     <div class="label-field-pair">
     <div class="label-container"><%= f.label :question %></div>
     <div class="input-container"> <%= f.text_field :question %></div>
     </div>
     <div class="label-field-pair">
     <div class="label-container"><%= f.label :answer1 %></div>
     <div class="input-container"> <%= f.text_field :answer1 %></div>
     </div>
     <div class="label-field-pair">
     <div class="label-container"><%= f.label :answer2 %></div>
     <div class="input-container"> <%= f.text_field :answer2 %></div>
     </div>
     <div class="label-field-pair">
     <div class="label-container"><%= f.label :answer3 %></div>
     <div class="input-container"> <%= f.text_field :answer3 %></div>
     </div>
     <div class="label-field-pair">
     <div class="label-container"><%= f.label :answer4 %></div>
     <div class="input-container"> <%= f.text_field :answer4 %></div>
     </div>

     <div class="label-field-pair">
     <div class="label-container"> <%= f.label :correct_option %></div>
     <div class="input-container"><%= f.select (:correct_option,%w{1,2,3,4}) %></div>
     </div>



     <%= f.submit "", :value => "► Save", :class => "submit_button" %>

     <% end %>
     </div>
     #server log
     Processing OnlineExamController#show (for 127.0.0.1 at 2011-04-05 19:31:00) [POST]
     Parameters: {"action"=>"show",      "authenticity_token"=>"OXozxkL1y+gCKkWxsUbAR8nH9Cb8MJJJPtnEVw8Y1AI=", "controller"=>"online_exam"}
     User Columns (0.8ms) SHOW FIELDS FROM `users`
     User Load (0.1ms) SELECT * FROM `users` WHERE (`users`.`id` = 8)
     CACHE (0.0ms) SELECT * FROM `users` WHERE (`users`.`id` = 8)
      Rendering online_exam/show
      Rendered online_exam/_question_bank (5.6ms)
      Completed in 21ms (View: 8, DB: 3) | 200 OK [http://localhost/online_exam/show]
       SQL (4.7ms) SET SQL_AUTO_IS_NULL=0


      Processing OnlineExamController#show (for 127.0.0.1 at 2011-04-05 19:31:02)      [POST]
       Parameters: {"action"=>"show",   "authenticity_token"=>"OXozxkL1y+gCKkWxsUbAR8nH9Cb8MJJJPtnEVw8Y1AI=", "controller"=>"online_exam"}
     User Columns (1.0ms) SHOW FIELDS FROM `users`
     User Load (0.8ms) SELECT * FROM `users` WHERE (`users`.`id` = 8)
     CACHE (0.0ms) SELECT * FROM `users` WHERE (`users`.`id` = 8)
     Rendering online_exam/show
     Rendered online_exam/_question_bank (5.6ms)
     Processing OnlineExamController#new_multiple_choice (for 127.0.0.1 at 2011-04-05 19:31:04) [POST]
     Parameters: {"action"=>"new_multiple_choice",   "authenticity_token"=>"OXozxkL1y+gCKkWxsUbAR8nH9Cb8MJJJPtnEVw8Y1AI=",  "controller"=>"online_exam"}
     User Columns (1.1ms) SHOW FIELDS FROM `users`
     User Load (0.1ms) SELECT * FROM `users` WHERE (`users`.`id` = 8)
     CACHE (0.0ms) SELECT * FROM `users` WHERE (`users`.`id` = 8)
       MultipleChoiceQuestion Columns (1.0ms) SHOW FIELDS FROM     `multiple_choice_questions`
     Completed in 22ms (View: 2, DB: 2) | 200 OK [http://localhost/online_exam/new_multiple_choice]
       SQL (0.1ms) SET SQL_AUTO_IS_NULL=0 here

我知道代码很长并且让我头疼了好几个小时,当我单击末尾的第一个链接时,我的问题 _new_multiple_choice.html.erb 没有呈现。

#online_exam_controller
class OnlineExamController < ApplicationController
   def index
    @user=current_user
    @employee=Employee.find_by_employee_number(@user.username)
    # flash[:notice]="#{@employee.id}"
    @subjects=EmployeesSubject.find_all_by_employee_id(@employee.id)
    @subject_name=[]
    @batch_details=[]
    @display_details=[[]]
    @count=0
    for @t in @subjects
     @subject_name[@count]=Subject.find(@t.subject_id,:select=>'name,code,batch_id')
     @batch_details[@count]=Batch.find(@subject_name[@count].batch_id,:select =>'name')
     @display_details[@count][0]=@subject_name[@count].name+" "+@batch_details[@count].name
     @display_details[@count][1][email protected]_id
     @count+=1
    end
   end
   def show
    if params[:subject_id]==''
     @question_type=[]
    else
     @question_type=['multiple_choice','fill_ups','matches','descriptive']
     @subject_id=params[:subject_id]

   end
   respond_to do |format|
   format.js {render:action=>'show' }
   end
  end
  def new_multiple_choice
   @quiz=MultipleChoiceQuestion.new
   @subject=Subject.find params[:id] if request.xhr? and params[:id]
   respond_to do |format|
    format.js {render :action => 'new_multiple_choice'}
   end
  end
 end
#index.html.erb
<div id="content-header">
<img src="/images/show_settings.png" alt="Subjects"/>
<h1>Subjects</h1>
<h3>Home</h3>
 <div id="app-back-button">
 <%= link_to_function image_tag("/images/buttons/back.png",:border => 0), "history.back()" %>
 </div>
 </div>

 <div id="page-yield">
 <% unless flash[:notice].nil? %>
 <p class="flash-msg"> <%= flash[:notice] %> </p>
 <% end %>
 <div class="box">

  <div class="label-field-pair">
  <label for="student_course">Select a batch:
  <%= image_tag("loader.gif",:align => "absmiddle",:border => 0,:id => "loader", :style =>"display: none;" ) %>
  </label>
  <div class="text-input-bg">
  <%= select :subject,:id,
   @display_details.map {|b| [b[0],b[1]]},
  {:prompt => 'selecct subject'},
  {:onchange =>"#{remote_function(
  :url=>{:action => 'show'},
  :before=> "Element.show('loader')",
  :success => "Element.hide('loader')" )}"}%>

  </div></div>

  <div id="subjects"></div>
  <div id="modal-box" style="display:none;"></div>

  <div class="extender"></div>
  </div></div>
  #show.rjs
  if @question_type.empty?
   page.replace_html 'subjects', :partial => 'question_bank'
  else
   page.replace_html 'subjects', :partial => 'question_bank'
  end
  #_question_bank.html.erb
  <% unless @question_type.nil? %>
   <div class="add_sub">
   <%= link_to_remote 'Add Multiple Question ', :url => { :action =>   'new_multiple_choice', :id => @subject_id } %>
   </div>
   <div class="add_sub">
    <%= link_to_remote 'Add Fill Up Question', :url => { :action => 'new_fill_ups', :id => @subject_id } %>
   </div>
   <div class="add_sub">
   <%= link_to_remote 'Add Match Question', :url => { :action => 'new_matches', :id => @subject_id } %>
   </div>
   <div class="add_sub">
   <%= link_to_remote 'Add Descriptive Question', :url => { :action => 'new_descriptive', :id => @subject_id } %>
   </div>
   <% end %>
  #new_multiple_choice.rjs
  page.replace_html 'modal-box', :partial => 'new_multiple_choice'
  page << "Modalbox.show($('modal-box'), {title: 'Add new multiple choice questions', width: 500});"
   #_new_multiple_choice.html.erb

   <div id="grading-levels-form">
   <h4><%= @subject.name unless @subject.nil? %></h4>

   <% form_remote_for @quiz do |f| %>

    <% subject_id = (@subject.nil? ? nil : @subject.id) %>
    <% batch_id = (@subject.nil? ? nil : @subject.batch_id ) %>
    <%= f.hidden_field :batch_id, :value => batch_id %>
    <%= f.hidden_field :subject_id, :value => elective_group_id %>

     <div id="form-errors"></div>

     <div class="label-field-pair">
     <div class="label-container"><%= f.label :question %></div>
     <div class="input-container"> <%= f.text_field :question %></div>
     </div>
     <div class="label-field-pair">
     <div class="label-container"><%= f.label :answer1 %></div>
     <div class="input-container"> <%= f.text_field :answer1 %></div>
     </div>
     <div class="label-field-pair">
     <div class="label-container"><%= f.label :answer2 %></div>
     <div class="input-container"> <%= f.text_field :answer2 %></div>
     </div>
     <div class="label-field-pair">
     <div class="label-container"><%= f.label :answer3 %></div>
     <div class="input-container"> <%= f.text_field :answer3 %></div>
     </div>
     <div class="label-field-pair">
     <div class="label-container"><%= f.label :answer4 %></div>
     <div class="input-container"> <%= f.text_field :answer4 %></div>
     </div>

     <div class="label-field-pair">
     <div class="label-container"> <%= f.label :correct_option %></div>
     <div class="input-container"><%= f.select (:correct_option,%w{1,2,3,4}) %></div>
     </div>



     <%= f.submit "", :value => "► Save", :class => "submit_button" %>

     <% end %>
     </div>
     #server log
     Processing OnlineExamController#show (for 127.0.0.1 at 2011-04-05 19:31:00) [POST]
     Parameters: {"action"=>"show",      "authenticity_token"=>"OXozxkL1y+gCKkWxsUbAR8nH9Cb8MJJJPtnEVw8Y1AI=", "controller"=>"online_exam"}
     User Columns (0.8ms) SHOW FIELDS FROM `users`
     User Load (0.1ms) SELECT * FROM `users` WHERE (`users`.`id` = 8)
     CACHE (0.0ms) SELECT * FROM `users` WHERE (`users`.`id` = 8)
      Rendering online_exam/show
      Rendered online_exam/_question_bank (5.6ms)
      Completed in 21ms (View: 8, DB: 3) | 200 OK [http://localhost/online_exam/show]
       SQL (4.7ms) SET SQL_AUTO_IS_NULL=0


      Processing OnlineExamController#show (for 127.0.0.1 at 2011-04-05 19:31:02)      [POST]
       Parameters: {"action"=>"show",   "authenticity_token"=>"OXozxkL1y+gCKkWxsUbAR8nH9Cb8MJJJPtnEVw8Y1AI=", "controller"=>"online_exam"}
     User Columns (1.0ms) SHOW FIELDS FROM `users`
     User Load (0.8ms) SELECT * FROM `users` WHERE (`users`.`id` = 8)
     CACHE (0.0ms) SELECT * FROM `users` WHERE (`users`.`id` = 8)
     Rendering online_exam/show
     Rendered online_exam/_question_bank (5.6ms)
     Processing OnlineExamController#new_multiple_choice (for 127.0.0.1 at 2011-04-05 19:31:04) [POST]
     Parameters: {"action"=>"new_multiple_choice",   "authenticity_token"=>"OXozxkL1y+gCKkWxsUbAR8nH9Cb8MJJJPtnEVw8Y1AI=",  "controller"=>"online_exam"}
     User Columns (1.1ms) SHOW FIELDS FROM `users`
     User Load (0.1ms) SELECT * FROM `users` WHERE (`users`.`id` = 8)
     CACHE (0.0ms) SELECT * FROM `users` WHERE (`users`.`id` = 8)
       MultipleChoiceQuestion Columns (1.0ms) SHOW FIELDS FROM     `multiple_choice_questions`
     Completed in 22ms (View: 2, DB: 2) | 200 OK [http://localhost/online_exam/new_multiple_choice]
       SQL (0.1ms) SET SQL_AUTO_IS_NULL=0 here

I know code is very long and been breaking my head for hours,My problem _new_multiple_choice.html.erb is not rendered when i click the first link at the end.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

要走干脆点 2024-11-06 11:41:33

大多数 Ruby 爱好者都会被这​​段代码吓坏并逃跑。这里不是回答你的问题(如果解决了,会让你的应用程序变得更加可怕),而是一些基础知识;这样做,这样你就可以开始提出人们会回答的问题。

Rails 会为您完成大部分工作,因此您不必自己查找与 id 的关系或设置数组。实例变量用于将事物传递到视图。尝试传递视图所需的内容,而不是更多。

首先,建立与模型的关系...

models/user.rb

class User
  has_one :employee
end

models/employee.rb

class Employee
  belongs_to :user
  has_and_belongs_to_many :employee_subjects
end

models/employee_subject.rb

class EmployeeSubject
  has_and_belongs_to_many :employees
  has_one :batch
end

models/batch.rb

class Batch
  belongs_to :employee_subject
end

has_and_belongs_to_many 有点棘手,所以请仔细阅读文档:
http://api.rubyonrails.org/classes/ActiveRecord /Associations/ClassMethods.html#method-i-has_and_belongs_to_many

然后您可以将控制器操作简化为一行...

controllers/online_exams_controller.rb

def index
  @subjects = current_user.employee.employee_subjects
end

并在您的视图中...

views/online_exams/index.html.erb

<ul>
<%= render :partial => '/subjects/list_view', :collection => @subjects, :as => :subject %>
</ul>

views/employee_subjects/_list_view.html.erb

<li>
  Name: <%= subject.name -%>, Code: <%= subject.code -%>,
  Batch Name: <%= subject.batch.name -%>
</li>

在进行更多操作之前,请考虑一下应用程序的对象应如何组织和关联,以及尝试使用有意义的名称。阅读有关面向对象编程概念的更多信息,并遵循一些 Rails 教程,并习惯用更少的代码行数制作应用程序!

Most Rubyists would be horrified by this code and run away. Instead of answering your question (which, if solved, would make your app even more horrific), here are some basics; do these so you can start asking questions that people would answer.

Rails does most of the work for you so you don't have to find relationships with ids or set arrays up yourself. Instance variables are used to pass things on to the view. Try to pass the view exactly what it needs and no more.

First, set up your relationships with the models...

models/user.rb

class User
  has_one :employee
end

models/employee.rb

class Employee
  belongs_to :user
  has_and_belongs_to_many :employee_subjects
end

models/employee_subject.rb

class EmployeeSubject
  has_and_belongs_to_many :employees
  has_one :batch
end

models/batch.rb

class Batch
  belongs_to :employee_subject
end

has_and_belongs_to_many is a bit tricky, so read the docs carefully:
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_and_belongs_to_many

Then you can simplify your controller actions to one line...

controllers/online_exams_controller.rb

def index
  @subjects = current_user.employee.employee_subjects
end

and in your views...

views/online_exams/index.html.erb

<ul>
<%= render :partial => '/subjects/list_view', :collection => @subjects, :as => :subject %>
</ul>

views/employee_subjects/_list_view.html.erb

<li>
  Name: <%= subject.name -%>, Code: <%= subject.code -%>,
  Batch Name: <%= subject.batch.name -%>
</li>

Before you do any more though, think through how your application's objects should be organised and related, and try to use names that make sense. Read more about object-oriented programming concepts, and follow some Rails tutorials, and get used making apps with 10 times fewer lines of code!

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