Rails:参数太少 [英] Rails: Too Few Arguments

查看:23
本文介绍了Rails:参数太少的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 Rails 应用程序中使用一些 Javascript.

我想让我的索引页允许我编辑索引页上的单个项目,然后在编辑时重新加载索引页.

我的 index.html.erb 页面如下所示:

<%=渲染'索引'%>

在我的 index.js.erb 中有:

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

在我的 holder_controller 中:

def 编辑持有人 = Holder.find(params[:id])结尾定义更新@holder = Holder.find(params[:id])如果@holder.update_attributes(params[:holder])format.html { redirect_to holder_path } #, flash[:success] = "holder updated")## ^---第28行错误格式.js别的渲染编辑"结尾结尾

当我加载索引页面时它很好.单击编辑按钮并提交表单后,我会得到以下信息:

但是如果我返回并刷新索引页面,编辑会被保存.我究竟做错了什么?

解决方案

你忘记写 responds_to 块:

def 更新@holder = Holder.find(params[:id])如果@holder.update_attributes(params[:holder])response_to do |格式|format.html { redirect_to holder_path } #, flash[:success] = "holder updated")格式.js结尾别的渲染编辑"结尾结尾

但我对您的 index.html.erb 表示怀疑,我认为这不会像您想象的那样真正起作用.

I am trying to get some Javascript working in my Rails app.

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:

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

In my index.js.erb I have:

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

and in my 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

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?

解决方案

You forgot to write responds_to block:

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.

这篇关于Rails:参数太少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆