Ruby-on-rails 论据太少
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11209900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Too Few Arguments
提问by Noah Clark
I am trying to get some Javascript working in my Rails app.
我正在尝试在我的 Rails 应用程序中运行一些 Javascript。
I want to have my index page allow me to edit individual items on the index page, and then reload the index page upon edit.
我想让我的索引页允许我编辑索引页上的单个项目,然后在编辑时重新加载索引页。
My index.html.erb page looks like:
我的 index.html.erb 页面如下所示:
<div id="index">
<%= render 'index' %>
</div>
In my index.js.erb I have:
在我的 index.js.erb 我有:
$('#index').html("<%=j render 'index' %>");
and in my holders_controller:
在我的 holder_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
When I load the index page it is fine. As soon as click the edit button and it submits the form, I get the following:
当我加载索引页面时,它很好。单击编辑按钮并提交表单后,我会得到以下信息:


But if I go back and refresh the index page, the edits are saved. What am I doing wrong?
但是如果我返回并刷新索引页面,则编辑会被保存。我究竟做错了什么?
回答by Matzi
You forgot to write responds_toblock:
你忘了写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
But I am suspicious about your index.html.erb, I don't think that will really work the way you think.
但是我对您的 index.html.erb 表示怀疑,我认为这不会像您认为的那样真正起作用。

