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 |'来使它工作。 –