参数“样本"的预期哈希(数组) [英] expected Hash (got Array) for param 'samples'

查看:107
本文介绍了参数“样本"的预期哈希(数组)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在跟踪嵌套形式和复杂形式的Railscasts集.在以单个表单创建多个模型的过程中,我能够编辑,更新,删除和创建嵌套在Batch模型中的示例模型的记录.

I have been following Railscasts episodes of Nested forms and complex forms. During the time of creating multiple model in a single form I was able to edit, update, delete and create records for sample models that were nested in the Batch model.

很长一段时间以来,我一直不知所措,也尝试过四处搜寻,但找不到解决此问题的正确方法.

I have been breaking my head from a long time and tried searching around as well but could not get any right solution for solving this problem.

我的开发日志文件给我以下错误.

my development log file gives me the following error.

错误消息:

Status: 500 Internal Server Error
  expected Hash (got Array) for param `samples'

在我的控制器中,我有这样的更新操作

in my controller I have the update action like this

def update
     @batch = Batch.find(params[:id])

     respond_to do |format|
       if @batch.update_attributes(params[:batch])
         flash[:notice] = 'Successfully updated Batch.'
         format.html { redirect_to(@batch) }
         format.xml  { head :ok }
       else
         format.html { render :action => "edit" }
         format.xml  { render :xml => @batch.errors, :status => :unprocessable_entity }
       end
     end
   end

我的看法是这样的:

<%= form_for @batch do |f| %>
......
<%= f.fields_for :samples do |s_form| %>
.... s_form things
<% end %>
<% end %>

我的模型包含相同的内容:

my model contains the same stuff :

has_many :samples, :dependent => :destroy

  accepts_nested_attributes_for :samples, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true

感谢所有建议.

推荐答案

对于遇到相同问题的其他人:

for others who met the same problem:

当您的表单中有两个字段时,会导致此错误:

this error is caused when you have two fields in your form like:

video: 'some string'
video['url']:  'some url'

然后导轨将崩溃,并显示以下错误:param的预期哈希值(获取字符串)

then rails will crash with the error: expected Hash (got String) for param

解决方案非常简单:将视频"更改为其他内容.例如:

the solution is quite simple: change 'video' to something else. e.g.:

video_origin_url: 'some string'
video['url']: 'some url'

这篇关于参数“样本"的预期哈希(数组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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