28

我想在我的Rails應用程序中使用一些Javascript。太少參數

我希望我的索引頁允許我編輯索引頁上的單個項目,然後在編輯時重新加載索引頁。

我index.html.erb頁的樣子:

<div id="index"> 
<%= render 'index' %> 
</div> 

在我index.js.erb的我:

$('#index').html("<%=j render 'index' %>"); 

,並在我的holders_controller:

def edit 
    holder = Holder.find(params[:id]) 
end 

def update 
    @holder = Holder.find(params[:id]) 
    if @holder.update_attributes(params[:holder]) 
    format.html { redirect_to holders_path } #, flash[:success] = "holder updated") 
    ## ^---Line 28 in error 
    format.js 
    else 
    render 'edit' 
    end 
end 

當我加載索引頁時,它很好。只要單擊編輯按鈕並提交表單時,我得到如下:

enter image description here

但是,如果我回去,並刷新索引頁,編輯內容被保存。我究竟做錯了什麼?

回答

89

你忘了寫responds_to塊:

def update 
    @holder = Holder.find(params[:id]) 
    if @holder.update_attributes(params[:holder]) 
    respond_to do |format| 
     format.html { redirect_to holders_path } #, flash[:success] = "holder updated") 
     format.js 
    end 
    else 
    render 'edit' 
    end 
end 

但我懷疑你的index.html.erb,我不認爲這會真正的工作,你的思維方式。

+4

'responds_to do' – clyfe

+0

對,謝謝。 :) – Matzi

+0

@Matzi我不得不使用'responds_to do | format |'來使它工作。 –