0
我有2個模型 - 安排和聯繫。Rails試圖在ajax調用後重新加載數據
在我/views/arrangements/index.html.erb頁,我想展現的聯繫人表。
我也有這種形式在它在我的安排一個示範窗口/ 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)) %>");
我之所以在安排new_contact形式/ index.html.erb是因爲我想有一個模式窗口,他們可以在排列頁面上添加新聯繫人,而無需轉到聯繫人頁面,添加聯繫人,然後返回安排頁面。 – Catfish 2013-03-10 17:03:07
好吧,我創建了一個名爲views/contacts/_running_list.html.erb的部分,並將我的表單代碼移動到該部分。當我嘗試添加一個新的聯繫人時,我收到錯誤「未定義的方法」爲空? for nil:NilClass'在_running_list部分的這一行上<%if to_contacts.empty? %>' – Catfish 2013-03-10 17:21:28
因爲你現在正在從JS局部渲染,所以你必須使用定義爲局部變量的變量而不是控制器動作中定義的實例變量。實例變量當然可以在您的JS.erb模板中使用。所以,嘗試通過這個當地人在你的電話渲染:部分 – Nerve 2013-03-10 19:10:13