我想根据某些参数创建一个新对象。
在我的new.html.haml
中%table
%thead
%tr
%td
= f.label: type
%td
= select_tag :question_type, options_for_select
%tbody#content
的javascript:
$('#question_type').change(function(){
$.ajax({
data: { question_type: $(this).val() },
url: window.location.href,
dataType: 'script'
});
});
在我的new.js.haml
中$('#content').html("#{escape_javascript(render(:partial => "question_form"))}");
_question_form.html.haml
中的
= form_for @question, :url => {:action => "create"} do |f|
%tr
%td{:style => 'text-align:right;'}
= f.label :name
%td
= f.text_field :name
%tr
%td
%td
= f.button :Submit, :value => 'Create'
在我的控制器中
def new
@question = Question.new
respond_to do |format|
format.html
format.js
end
end
def create
@question.save
respond_to do |format|
format.html (redirect_to questions_path)
format.js
end
end
一切正常,但输入问题名称后我无法提交表格。如何提交此表单?
答案 0 :(得分:1)
问题在于你的html结构。你应该把表格放在桌子外面。
= form_for @question do |f|
%table
请注意,这将提交您在创建操作中可以忽略的问题类型
更新:
# new.html.haml
#form-content= render 'question_form'
# _question_form.html.haml
= form_for @question, :url => {:action => "create"} do |f|
%table
%thead
%tr
%td= f.label: type
%td= select_tag :question_type, options_for_select
%tbody
- if params[:question_type].present?
%tr
%td{:style => 'text-align:right;'}= f.label :name
%td= f.text_field :name
%tr
%td
%td= f.button :Submit, :value => 'Create'
# new.js.haml
$('#form-content').html("#{escape_javascript render("question_form")}");