Skip to content
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

Is it possible to work with serializable fields (Hash)? #57

Open
qd3v opened this issue Oct 1, 2014 · 1 comment
Open

Is it possible to work with serializable fields (Hash)? #57

qd3v opened this issue Oct 1, 2014 · 1 comment

Comments

@qd3v
Copy link

qd3v commented Oct 1, 2014

Hi!
For two hours reading specs and trying to permit hash, without luck:

# Model
class Pass
  serialize :header, Hash
  serialize :primary, Hash

  protect do
    can :update
  end
end

# Ctrl
def update
  respond_with pass.tap { |p| p.restrict!.update(params[:pass]) }
end

# Incoming params(pure)

{"id"=>"1095", "name"=>"Name 1", "logo_text"=>"Logo text 1", "header"=>{},
"primary"=>{"key"=>"primary_key", "label"=>"primary_label", "value"=>"primary_value"}, 
"icon_url"=>nil, "logo_url"=>nil, "strip_url"=>nil, 

"pass"=>{"id"=>"1095", "name"=>"Name 1", "logo_text"=>"Logo text 1", "header"=>{}, 
"primary"=>{"key"=>"primary_key", "label"=>"primary_label", "value"=>"primary_value"}}}

Started PUT "/pass/1095?format=json" for 127.0.0.1 at 2014-10-01 06:25:57 +0400
Processing by PassController#update as JSON
(0.7ms) BEGIN
Unpermitted parameters: header, primary # <---- not :name or other plain fields
  SQL (0.2ms)  UPDATE "passes" SET "header" = $1, "primary" = $2 WHERE "passes"."id" = 1095  [["header", "--- {}\n"], ["primary", "--- {}\n"]]
(0.6ms)  COMMIT

No transformations of params or anything else was done. Plain stupid action. Where do you think the problem is?

protector (0.7.7)
rails (4.1.6)

I really want to switch to protector, 'cause SP is clumsy and hardtestable for me.
BTW, in case of hash, how to permit only its existence (parameter name), not its details, such as hash keys or even worse key values?
Any help will be appreciated, thanks!

@toxix
Copy link
Contributor

toxix commented Jan 24, 2015

Try to change your controller like this and things should work:

def update
  respond_with pass.tap { |p| p.restrict!.update(params[:pass].require(:pass)) }
end

best regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants