-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for ActiveModel ActionController::Parameters (ActiveModel::ForbiddenAttributesError with active_model 4) #404
Comments
You should build a repro and post it somewhere. I think few people here had a chance to really try Rails 4 + Grape out, so I would imagine this needs fixing or some other changes. |
Right, I have created a test repository |
I took a look at this. In Rails 4 controllers you're supposed to do this: def post_params
# This says that params[:user] is required, but inside that, only params[:user][:name] and params[:user][:email] are permitted
# Unpermitted params will be stripped out
params.require(:user).permit(:name, :email)
end Your implementation will do fine and is equivalent to this, albeit not "external" and certainly not pretty. So following this model Grape would need to allow something similar at the params do
group :user do
requires :name
requires :email
end
end It's a similar structure that would need to turn into |
I looked into the issue a little bit more and it looks like it actually should work without changing grape. |
Interesting. There's obviously a straightforward monkey patch to prevent this behavior. Do you think there's a better solution? |
If Mash wouldn't respond to "permitted?" monkey patch won't be required. |
As an external gem, sure. Grape would gladly accomodate an extension point to make it cleaner. |
I run in the exact same thing.
It would be great to use the params dsl to wrap strong_params. required, optional define the permited fields, etc. |
Mash is just a fancy hash. On Tue, Jul 23, 2013 at 8:09 AM, Manuel Wiedenmann
|
seems to be too much work ;) |
An easier solution for the time being (permits only the parameters you’ve defined anyways!) helpers do
def permitted_params
ActionController::Parameters.new(params).permit(params['route_info'].route_params.keys)
end
end Then you can just utilise permitted_params. |
You can use def permitted_params
declared(params)
end |
For people looking for a quick solution: helpers do
def declared_params
declared(params, include_missing: false)
end
end
params do
requires :important_field
optional :unimportant_field
end
post 'objects/:id' do
object = Object.new(declared_params)
# save and present
end Important: if you don't set |
The hashie_rails gem seems to resolve this issue |
@robjacoby yep it does forgot to close the issue. |
@robjacoby , @Maxim-Filimonov good information, thx guys! |
@dm1try @Maxim-Filimonov @dblock Could we re-open this issue? It feels a little hacky to "fix" this by adding hashie-rails. We're also seeing this issue in Napa, so it's not quite just Rails thing (probably ActiveModel or AR). Also, anyone know what version of Grape this issue started popping up? |
@shaqq hashie-rails is not rails specific anymore. Sorry for confusion, It works with just ActiveSupport |
I think that yes, the gem should be renamed or we're going to have confused users for a long time :) Maybe something like |
@shaqq as you can see above, it's related to the interaction between
The |
I'm having issue with grape mounted inside rails 4 application. Whenever I try to use a model backed by activerecord with grape I get ActiveModel::ForbiddenAttributesError. I found a workaround which couples grape to Rails ActionController:
Am I doing something wrong or it requires PR to fix?
The text was updated successfully, but these errors were encountered: