我有2个型号 - 安排和联系。
在我的 /views/arrangements/index.html.erb 页面中,我正在尝试显示联系人表格。
我的 arrange / index.html.erb 还有一个带有此表单的模型窗口:
<%= form_for @new_to_contact, :remote => true, :id => 'new_item' do |f| %>
<%= f.label :family_name %>
<%= f.text_field :family_name %>
<%= f.label :action %>
<%= f.text_area :action, :rows => 3 %>
<%= button_tag "Save", :type => 'submit' %>
<%= button_tag "Cancel", :type => 'button', :data => {:dismiss => 'modal' } %>
<% end %>
这是我的arrangements_controller#index
方法
def index
@arrangements = Arrangement.find(:all, :order => 'location_id')
@new_to_contact = Contact.new
respond_to do |format|
format.html # index.html.erb
format.json { render json: @arrangements }
end
end
这是我的contacts_controller#create
方法
def create
@to_contact = Contact.new(params[:to_contact])
respond_to do |format|
if @to_contact.save
format.js
format.html { redirect_to @to_contact, notice: 'To contact was successfully created.' }
format.json { render json: @to_contact, status: :created, location: @to_contact }
else
format.html { render action: "new" }
format.json { render json: @to_contact.errors, status: :unprocessable_entity }
end
end
end
这是我的 /views/contacts/create.js.erb
console.log('TEST');
$('#myModal').modal('hide');
我的问题是,如何从我的 create.js.erb 文件中重新加载我的安排索引文件中的联系人列表?
我尝试将此行添加到 create.js.erb 文件中,但这会引发模板错误:
$(".table").html("<%= escape_javascript(render(Contact.all)) %>");
答案 0 :(得分:1)
根据Rails的方式,您应该将您的new_contact表单从arrangements / index.html.erb提取到contacts / _form.html.erb,并在arrange / index.html.erb中呈现此部分。
然后,您可以使用以下内容轻松地从js.erb文件渲染此模板:
$('.table tr:last').after("<%= escape_javascript(render :partial => 'form', :locals => {:contact => @to_contact}) %>")