Rails controller params validation by dry-validation










0















First time I'am trying using dry validation gem.



I want to create a form for multi-models (User, Address, Company)



I decided to use a dry validator but I can't validate the form. I receive validator error ":user=>["must be a hash"]"



How to make it work?



app/controller/users_controller.rb



class UsersController < ApplicationController
def new
@user = User.new
@user.address = Address.new
@user.company = Company.new
@user.company.address = Address.new
end

def create
@user = User.new
validation = UserValidator::UserSchema.call(params)
if validation.success?
@user.attributes = validation.output[:user]
@user.save
redirect_to new_user_path, notice: 'Form send!'
else
redirect_to new_user_path, warning: "#validation.errors"
end
end
end


app/models/user.rb



class User < ApplicationRecord
has_one :company
has_one :address, as: :addressable
accepts_nested_attributes_for :company, :address
end


app/validators/user_validator



class UserValidator
UserSchema = Dry::Validation.Params do
required(:user).schema do
required('first_name').filled
end
end
end


print params output in create action:



<ActionController::Parameters "utf8"=>"✓","authenticity_token"=>"Dszr0k90aklK1NC4uGcemAl+yFa9ppMDo/gCLJt2wC1WjTgLC+NFebRqm6iqtVTnQzRgd7v0icntxETbxJ7v9g==", "user"=>"first_name"=>"Test", "last_name"=>"asdasdasdasd", "email_address"=>"sadasdasd", "date_of_birth"=>"sdasda", "phone_number"=>"", "address_attributes"=>"street"=>"dadadadsdad", "city"=>"asdasdad", "zip_code"=>"dasd", "country"=>"adsa", "company_attributes"=>"name"=>"", "address_attributes"=>"street"=>"", "city"=>"", "zip_code"=>"", "country"=>"", "commit"=>"Submit", "controller"=>"users", "action"=>"create" permitted: false>









share|improve this question
























  • What does params contain in the create action?

    – Sergio Tulentsev
    Nov 13 '18 at 9:38












  • I added params output in question

    – Wojtek Kostański
    Nov 13 '18 at 9:50











  • Does it not like AC::Parameters? What if you try UserValidator::UserSchema.call(params.to_h) or UserValidator::UserSchema.call(params[:user].to_h)?

    – Sergio Tulentsev
    Nov 13 '18 at 9:57











  • I received 'unable to convert unpermitted parameters to hash' error

    – Wojtek Kostański
    Nov 13 '18 at 10:20






  • 1





    Try params.permit! then

    – Sergio Tulentsev
    Nov 13 '18 at 10:24















0















First time I'am trying using dry validation gem.



I want to create a form for multi-models (User, Address, Company)



I decided to use a dry validator but I can't validate the form. I receive validator error ":user=>["must be a hash"]"



How to make it work?



app/controller/users_controller.rb



class UsersController < ApplicationController
def new
@user = User.new
@user.address = Address.new
@user.company = Company.new
@user.company.address = Address.new
end

def create
@user = User.new
validation = UserValidator::UserSchema.call(params)
if validation.success?
@user.attributes = validation.output[:user]
@user.save
redirect_to new_user_path, notice: 'Form send!'
else
redirect_to new_user_path, warning: "#validation.errors"
end
end
end


app/models/user.rb



class User < ApplicationRecord
has_one :company
has_one :address, as: :addressable
accepts_nested_attributes_for :company, :address
end


app/validators/user_validator



class UserValidator
UserSchema = Dry::Validation.Params do
required(:user).schema do
required('first_name').filled
end
end
end


print params output in create action:



<ActionController::Parameters "utf8"=>"✓","authenticity_token"=>"Dszr0k90aklK1NC4uGcemAl+yFa9ppMDo/gCLJt2wC1WjTgLC+NFebRqm6iqtVTnQzRgd7v0icntxETbxJ7v9g==", "user"=>"first_name"=>"Test", "last_name"=>"asdasdasdasd", "email_address"=>"sadasdasd", "date_of_birth"=>"sdasda", "phone_number"=>"", "address_attributes"=>"street"=>"dadadadsdad", "city"=>"asdasdad", "zip_code"=>"dasd", "country"=>"adsa", "company_attributes"=>"name"=>"", "address_attributes"=>"street"=>"", "city"=>"", "zip_code"=>"", "country"=>"", "commit"=>"Submit", "controller"=>"users", "action"=>"create" permitted: false>









share|improve this question
























  • What does params contain in the create action?

    – Sergio Tulentsev
    Nov 13 '18 at 9:38












  • I added params output in question

    – Wojtek Kostański
    Nov 13 '18 at 9:50











  • Does it not like AC::Parameters? What if you try UserValidator::UserSchema.call(params.to_h) or UserValidator::UserSchema.call(params[:user].to_h)?

    – Sergio Tulentsev
    Nov 13 '18 at 9:57











  • I received 'unable to convert unpermitted parameters to hash' error

    – Wojtek Kostański
    Nov 13 '18 at 10:20






  • 1





    Try params.permit! then

    – Sergio Tulentsev
    Nov 13 '18 at 10:24













0












0








0








First time I'am trying using dry validation gem.



I want to create a form for multi-models (User, Address, Company)



I decided to use a dry validator but I can't validate the form. I receive validator error ":user=>["must be a hash"]"



How to make it work?



app/controller/users_controller.rb



class UsersController < ApplicationController
def new
@user = User.new
@user.address = Address.new
@user.company = Company.new
@user.company.address = Address.new
end

def create
@user = User.new
validation = UserValidator::UserSchema.call(params)
if validation.success?
@user.attributes = validation.output[:user]
@user.save
redirect_to new_user_path, notice: 'Form send!'
else
redirect_to new_user_path, warning: "#validation.errors"
end
end
end


app/models/user.rb



class User < ApplicationRecord
has_one :company
has_one :address, as: :addressable
accepts_nested_attributes_for :company, :address
end


app/validators/user_validator



class UserValidator
UserSchema = Dry::Validation.Params do
required(:user).schema do
required('first_name').filled
end
end
end


print params output in create action:



<ActionController::Parameters "utf8"=>"✓","authenticity_token"=>"Dszr0k90aklK1NC4uGcemAl+yFa9ppMDo/gCLJt2wC1WjTgLC+NFebRqm6iqtVTnQzRgd7v0icntxETbxJ7v9g==", "user"=>"first_name"=>"Test", "last_name"=>"asdasdasdasd", "email_address"=>"sadasdasd", "date_of_birth"=>"sdasda", "phone_number"=>"", "address_attributes"=>"street"=>"dadadadsdad", "city"=>"asdasdad", "zip_code"=>"dasd", "country"=>"adsa", "company_attributes"=>"name"=>"", "address_attributes"=>"street"=>"", "city"=>"", "zip_code"=>"", "country"=>"", "commit"=>"Submit", "controller"=>"users", "action"=>"create" permitted: false>









share|improve this question
















First time I'am trying using dry validation gem.



I want to create a form for multi-models (User, Address, Company)



I decided to use a dry validator but I can't validate the form. I receive validator error ":user=>["must be a hash"]"



How to make it work?



app/controller/users_controller.rb



class UsersController < ApplicationController
def new
@user = User.new
@user.address = Address.new
@user.company = Company.new
@user.company.address = Address.new
end

def create
@user = User.new
validation = UserValidator::UserSchema.call(params)
if validation.success?
@user.attributes = validation.output[:user]
@user.save
redirect_to new_user_path, notice: 'Form send!'
else
redirect_to new_user_path, warning: "#validation.errors"
end
end
end


app/models/user.rb



class User < ApplicationRecord
has_one :company
has_one :address, as: :addressable
accepts_nested_attributes_for :company, :address
end


app/validators/user_validator



class UserValidator
UserSchema = Dry::Validation.Params do
required(:user).schema do
required('first_name').filled
end
end
end


print params output in create action:



<ActionController::Parameters "utf8"=>"✓","authenticity_token"=>"Dszr0k90aklK1NC4uGcemAl+yFa9ppMDo/gCLJt2wC1WjTgLC+NFebRqm6iqtVTnQzRgd7v0icntxETbxJ7v9g==", "user"=>"first_name"=>"Test", "last_name"=>"asdasdasdasd", "email_address"=>"sadasdasd", "date_of_birth"=>"sdasda", "phone_number"=>"", "address_attributes"=>"street"=>"dadadadsdad", "city"=>"asdasdad", "zip_code"=>"dasd", "country"=>"adsa", "company_attributes"=>"name"=>"", "address_attributes"=>"street"=>"", "city"=>"", "zip_code"=>"", "country"=>"", "commit"=>"Submit", "controller"=>"users", "action"=>"create" permitted: false>






ruby-on-rails validation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 9:49







Wojtek Kostański

















asked Nov 13 '18 at 9:15









Wojtek KostańskiWojtek Kostański

515




515












  • What does params contain in the create action?

    – Sergio Tulentsev
    Nov 13 '18 at 9:38












  • I added params output in question

    – Wojtek Kostański
    Nov 13 '18 at 9:50











  • Does it not like AC::Parameters? What if you try UserValidator::UserSchema.call(params.to_h) or UserValidator::UserSchema.call(params[:user].to_h)?

    – Sergio Tulentsev
    Nov 13 '18 at 9:57











  • I received 'unable to convert unpermitted parameters to hash' error

    – Wojtek Kostański
    Nov 13 '18 at 10:20






  • 1





    Try params.permit! then

    – Sergio Tulentsev
    Nov 13 '18 at 10:24

















  • What does params contain in the create action?

    – Sergio Tulentsev
    Nov 13 '18 at 9:38












  • I added params output in question

    – Wojtek Kostański
    Nov 13 '18 at 9:50











  • Does it not like AC::Parameters? What if you try UserValidator::UserSchema.call(params.to_h) or UserValidator::UserSchema.call(params[:user].to_h)?

    – Sergio Tulentsev
    Nov 13 '18 at 9:57











  • I received 'unable to convert unpermitted parameters to hash' error

    – Wojtek Kostański
    Nov 13 '18 at 10:20






  • 1





    Try params.permit! then

    – Sergio Tulentsev
    Nov 13 '18 at 10:24
















What does params contain in the create action?

– Sergio Tulentsev
Nov 13 '18 at 9:38






What does params contain in the create action?

– Sergio Tulentsev
Nov 13 '18 at 9:38














I added params output in question

– Wojtek Kostański
Nov 13 '18 at 9:50





I added params output in question

– Wojtek Kostański
Nov 13 '18 at 9:50













Does it not like AC::Parameters? What if you try UserValidator::UserSchema.call(params.to_h) or UserValidator::UserSchema.call(params[:user].to_h)?

– Sergio Tulentsev
Nov 13 '18 at 9:57





Does it not like AC::Parameters? What if you try UserValidator::UserSchema.call(params.to_h) or UserValidator::UserSchema.call(params[:user].to_h)?

– Sergio Tulentsev
Nov 13 '18 at 9:57













I received 'unable to convert unpermitted parameters to hash' error

– Wojtek Kostański
Nov 13 '18 at 10:20





I received 'unable to convert unpermitted parameters to hash' error

– Wojtek Kostański
Nov 13 '18 at 10:20




1




1





Try params.permit! then

– Sergio Tulentsev
Nov 13 '18 at 10:24





Try params.permit! then

– Sergio Tulentsev
Nov 13 '18 at 10:24












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277535%2frails-controller-params-validation-by-dry-validation%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277535%2frails-controller-params-validation-by-dry-validation%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3