I have a ruby application using Grape, and it doesn't have rails.
class Article < ActiveRecord::Base
end
class API::Articles < Grape::API
post '/articles' do
article = Article.create(params[:article])
end
end
Article.create
gives ActiveModel::ForbiddenAttributesError:
There is some discussion about it here, but I don't understand it. I've tried this suggestion:
post '/articles' do
article = Article.create(permitted_params[:article])
represent(article, env)
end
helpers do
def permitted_params
@permitted_params ||= declared(params, include_missing: false)
end
end
This time @permitted_params
is empty so attributes are gone.
I've also tried wrapping th hash with ActionController::Parameters
, but that fails with other errors.
What is the suggested solution to solve ForbiddenAttributesError
, in Grape as of now?
Grape uses hashie gem for params
, and their solution for this is to include a gem called hashie_rails, but this gem brings all the rails with it, but I don't want any of that. So I need a vanilla solution.
https://gist.github.com/smd686s/6320643
Gemfile
app.rb