expected Hash (got Array) for param 'samples'

匆匆过客 提交于 2019-12-10 12:43:30

问题


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.

ERROR MESSAGE:

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

my view is something like this:

<%= 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

All suggestions are appreciated.


回答1:


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'

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'



回答2:


I had the same problem, and just fixed it.

Check the headers of your request. I mine I saw:

weight[2][name]:Tests
weight[2][value]:75
weight[1][name]:Quizzes
weight[1][value]:25
weight[][name]:Foo
weight[][value]:

It was the last two which caused the issue. In my case I had to give this weight an ID to get rid of the error.



来源:https://stackoverflow.com/questions/9259210/expected-hash-got-array-for-param-samples

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!