Ruby on rails 如何将形式为的对象传递到文件`action\u name.js.erb中的部分渲染`
我使用了一个动作来显示一门课程的所有主题,当该课程被选中时。现在我想使用virtual属性在表单上显示一个字段Ruby on rails 如何将形式为的对象传递到文件`action\u name.js.erb中的部分渲染`,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我使用了一个动作来显示一门课程的所有主题,当该课程被选中时。现在我想使用virtual属性在表单上显示一个字段number\u question,每个要使用的主题都可以输入一个数字。我的模型: class GeneralExam < ActiveRecord::Base attr_accessible :course_id, :description attr_accessor :numbe_question end 我的更新_topics.js.erb $('#list_top
number\u question
,每个要使用的主题都可以输入一个数字。我的模型:
class GeneralExam < ActiveRecord::Base
attr_accessible :course_id, :description
attr_accessor :numbe_question
end
我的更新_topics.js.erb
$('#list_topics').html("<%= j (render('general_exams/list_topics')) %>")
但是如果我想显示输入字段,我必须有一个f
对象,但是当我渲染\u列表\u主题时,我没有。因此,我想问一下如何将表单的f
对象传递给部分\u列表\u主题
或者有人可以告诉我如何使用jquery在表单的h5
元素之后添加输入字段,非常感谢。我认为有一些不同的方法可以解决这个问题。其中两个(我:
1) 您可以定义一个不同的表单,将数字问题数据发布到另一个控制器操作。
2) 当用户完成字段值的更新时,使用jquery的更改回调提交数字问题
我认为第一种选择更好,更容易编码。你会得到这样的结果:
<%= simple_form_for @general_exam, defaults: { error: false } do |f| %>
<fieldset>
<legend>General Exam information</legend>
<%= render 'shared/error_messages', object: f.object %>
<%= f.association :course, collection: current_user.courses, prompt: "Choose Course for exam" %>
<%= f.input :name %>
<%= f.input :description, input_html: { rows: 2, class: 'span6' } %>
</fieldset>
<%= f.submit class: "new_resource" %>
<% end %>
一般考试资料
然后,您将在同一模板上有一个不同的表单(用于提交主题编号):
您将继续使用JS partial进行响应,根据主题数据替换div的内容 我认为有一些不同的方法来解决这个问题。其中两个(我:
1) 您可以定义一个不同的表单,将数字问题数据发布到另一个控制器操作。
2) 当用户完成字段值的更新时,使用jquery的更改回调提交数字问题
我认为第一种选择更好,更容易编码。你会得到这样的结果:
<%= simple_form_for @general_exam, defaults: { error: false } do |f| %>
<fieldset>
<legend>General Exam information</legend>
<%= render 'shared/error_messages', object: f.object %>
<%= f.association :course, collection: current_user.courses, prompt: "Choose Course for exam" %>
<%= f.input :name %>
<%= f.input :description, input_html: { rows: 2, class: 'span6' } %>
</fieldset>
<%= f.submit class: "new_resource" %>
<% end %>
一般考试资料
然后,您将在同一模板上有一个不同的表单(用于提交主题编号):
您将继续使用JS partial进行响应,根据主题数据替换div的内容 但如果是这样的话,我现在就有两个按钮提交?我还不清楚:(不…在同一页上有两个不同的表单。但是我如何创建输入字段呢?就像我有一个表单一样。整个第二个表单将在你的部分(.js.erb)中文件我希望每个主题都有一个输入字段,但渲染时,我可能只会显示一个输入字段的所有主题,但如果是这样,现在在表单上,我将有两个按钮“提交”?我还不清楚:(不…在同一页上有两个不同的表单。但是我如何创建输入字段?就像我有一个表单一样。整个第二个表单将在部分(.js.erb)文件中。我希望每个主题都有一个输入字段,但渲染时,我可能只显示一个输入字段的所有主题
<% @topics.each do |topic| %>
<h5><%= topic.name %></h3>
<%# f.input :number_question, input_html: { name: "number_question[#{topic.id}]" } %>
<% end %>
<%= simple_form_for @general_exam, defaults: { error: false } do |f| %>
<fieldset>
<legend>General Exam information</legend>
<%= render 'shared/error_messages', object: f.object %>
<%= f.association :course, collection: current_user.courses, prompt: "Choose Course for exam" %>
<%= f.input :name %>
<%= f.input :description, input_html: { rows: 2, class: 'span6' } %>
</fieldset>
<%= f.submit class: "new_resource" %>
<% end %>
<%= simple_form_for @general_exam, url: whatever_is_your_new_topics_number_path, defaults: { error: false } do |f| %>
<fieldset>
<div id="list_topics">
</div>
</fieldset>
<% end %>