Nested strong parameters in rails - AssociationTypeMismatch MYMODEL expected, got ActionController::Parameters()

好久不见. 提交于 2019-12-04 01:24:21
user3712451

I'm using Angular.js & Rails & Rails serializer, and this worked for me:

Model:

  • has_many :features
  • accepts_nested_attributes_for :features

ModelSerializer:

  • has_many :features, root: :features_attributes

Controller:

  • params.permit features_attributes: [:id, :enabled]

AngularJS:

  • ng-repeat="feature in model.features_attributes track by feature.id

Are you using accepts_nested_attributes_for :books on your project model? If so, instead of "books", the key should be "books_attributes".

def project_params
  params.permit(:id, :complete, :false, :private, books_attributes: [:id, :name])
end

My solution to this using ember.js was setting the books_attributes mannualy.

In controller:

def project_params      
  params[:project][:books_attributes] = params[:project][:books_or_whatever_name_relationships_have] if params[:project][:books_or_whatever_name_relationships_have]
  params.require(:project).permit(:attr1, :attr2,...., books_attributes: [:book_attr1, :book_attr2, ....])

end

So rails checks and filters the nested attributes as it expected them to come

This worked for me. My parent model was an Artist and the child model was a Url.

class ArtistsController < ApplicationController

  def update
    artist = Artist.find(params[:id].to_i)
    artist.update_attributes(artist_params)
    render json: artist
  end

private

  def artist_params
    remap_urls(params.permit(:name, :description, urls: [:id, :url, :title, :_destroy]))
  end

  def remap_urls(hash)
    urls = hash[:urls]
    return hash unless urls
    hash.reject{|k,v| k == 'urls' }.merge(:urls_attributes => urls)
  end
end

class Artist < ActiveRecord::Base
  has_many :urls, dependent: :destroy
  accepts_nested_attributes_for :urls, allow_destroy: true
end

class Url < ActiveRecord::Base
  belongs_to :artist
end

... and in coffeescript (to handle deletions):

  @ArtistCtrl = ($scope, $routeParams, $location, API) ->

    $scope.destroyUrls = []

    $scope.update = (artist) ->
      artist.urls.push({id: id, _destroy: true}) for id in $scope.destroyUrls
      artist.$update(redirectToShow, artistError)

    $scope.deleteURL = (artist,url) ->
      artist.urls.splice(artist.urls.indexOf(url),1)
      $scope.destroyUrls.push(url.id)

Wasted several days trying to figure out how to use accepts_nested_attributes with Angular, and the issue is always the same: Rails whitelist will not allow the variables into the params hash. I've tried every single different whitelisting syntax that everyone said on SO and other blogs, tried using :inverse, tried using habtm and mas_many_through, tried manually rolling my own solution but that wont work if the whitelist wont allow params through, tried doing what http://guides.rubyonrails.org says about 'Outside the Scope of Strong Parameters', tried removing whitelisting all together which isnt really an option but it causes other problems anyways. Not sure why rails 4 strong parameter whitelisting wont allow arbitrary data thru, thats a huge problem especially if accepts_nested_attributes doesn't work either.... I guess we are left to just create/delete all associations on a separate page/form/controller and look like an idiot making my end users use several forms/pages to do something that should be easily doable on 1 page with 1 form. Ya know, usually I expect Angular to screw me, but this time Angular worked quite well and it was actually Rails 4 that screwed me twice on 1 issue that should be very straightforward.

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