rails cannot pass nested hash to controller params











up vote
0
down vote

favorite












Since I am using Rspec to test whether my controller is working fine, I did things below:



in test_rspec.rb,



before do
params = {
date_begin: "2018-10-01",
date_end: "2018-10-05",
options: {country_code: "US"}
}

get :index, params: params, as: :json
end

it do
expected(response).to have_http_response(200) }
end


in test_controller.rb,



def index
puts params_checker
render json: Test.report(params_checker[:date_begin],
params_checker[:date_end],
params_checker[:options])
end

private
def params_checker
params.permit(:date_begin, :date_end, :options)
end


when I run code with rspec command, there goes something wrong, the parameter with nested hash :options is gone! below is the output of params in terminal & some of its error info:



> {"date_begin"=>"2018-10-01", "date_end"=>"2018-10-03"}
> ArgumentError:
wrong number of arguments (given 3, expected 2)


I search everywhere on internet and tried some of the solutions but it won`t help. I do know this error is caused by parameter missing. Could any one help me finger it out why hashes cannot be passed to controller`s params in my case?



note: I am using rails 5.1.6










share|improve this question




















  • 1




    1) Change options={country_code: "US"} to options: {country_code: "US"} 2) Test.report receives only 2 arguments, you are passing 3
    – chumakoff
    Nov 7 at 11:37










  • @chumakoff after I change options to options: {country_code: "US"}.to_s and in controller eval(params[:options]) it worked fine. but this is not a really good way to solve the problem.
    – weird honey
    Nov 8 at 2:02










  • Are you kidding me? You just created another problem! It must be like this: params.permit(:date_begin, :date_end, options: [:country_code]) and without .to_s
    – chumakoff
    Nov 8 at 8:51










  • @chumakoff please see the answer below, method in comment was a temporary solution and now I replaced it with which in my answer.
    – weird honey
    Nov 8 at 10:30















up vote
0
down vote

favorite












Since I am using Rspec to test whether my controller is working fine, I did things below:



in test_rspec.rb,



before do
params = {
date_begin: "2018-10-01",
date_end: "2018-10-05",
options: {country_code: "US"}
}

get :index, params: params, as: :json
end

it do
expected(response).to have_http_response(200) }
end


in test_controller.rb,



def index
puts params_checker
render json: Test.report(params_checker[:date_begin],
params_checker[:date_end],
params_checker[:options])
end

private
def params_checker
params.permit(:date_begin, :date_end, :options)
end


when I run code with rspec command, there goes something wrong, the parameter with nested hash :options is gone! below is the output of params in terminal & some of its error info:



> {"date_begin"=>"2018-10-01", "date_end"=>"2018-10-03"}
> ArgumentError:
wrong number of arguments (given 3, expected 2)


I search everywhere on internet and tried some of the solutions but it won`t help. I do know this error is caused by parameter missing. Could any one help me finger it out why hashes cannot be passed to controller`s params in my case?



note: I am using rails 5.1.6










share|improve this question




















  • 1




    1) Change options={country_code: "US"} to options: {country_code: "US"} 2) Test.report receives only 2 arguments, you are passing 3
    – chumakoff
    Nov 7 at 11:37










  • @chumakoff after I change options to options: {country_code: "US"}.to_s and in controller eval(params[:options]) it worked fine. but this is not a really good way to solve the problem.
    – weird honey
    Nov 8 at 2:02










  • Are you kidding me? You just created another problem! It must be like this: params.permit(:date_begin, :date_end, options: [:country_code]) and without .to_s
    – chumakoff
    Nov 8 at 8:51










  • @chumakoff please see the answer below, method in comment was a temporary solution and now I replaced it with which in my answer.
    – weird honey
    Nov 8 at 10:30













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Since I am using Rspec to test whether my controller is working fine, I did things below:



in test_rspec.rb,



before do
params = {
date_begin: "2018-10-01",
date_end: "2018-10-05",
options: {country_code: "US"}
}

get :index, params: params, as: :json
end

it do
expected(response).to have_http_response(200) }
end


in test_controller.rb,



def index
puts params_checker
render json: Test.report(params_checker[:date_begin],
params_checker[:date_end],
params_checker[:options])
end

private
def params_checker
params.permit(:date_begin, :date_end, :options)
end


when I run code with rspec command, there goes something wrong, the parameter with nested hash :options is gone! below is the output of params in terminal & some of its error info:



> {"date_begin"=>"2018-10-01", "date_end"=>"2018-10-03"}
> ArgumentError:
wrong number of arguments (given 3, expected 2)


I search everywhere on internet and tried some of the solutions but it won`t help. I do know this error is caused by parameter missing. Could any one help me finger it out why hashes cannot be passed to controller`s params in my case?



note: I am using rails 5.1.6










share|improve this question















Since I am using Rspec to test whether my controller is working fine, I did things below:



in test_rspec.rb,



before do
params = {
date_begin: "2018-10-01",
date_end: "2018-10-05",
options: {country_code: "US"}
}

get :index, params: params, as: :json
end

it do
expected(response).to have_http_response(200) }
end


in test_controller.rb,



def index
puts params_checker
render json: Test.report(params_checker[:date_begin],
params_checker[:date_end],
params_checker[:options])
end

private
def params_checker
params.permit(:date_begin, :date_end, :options)
end


when I run code with rspec command, there goes something wrong, the parameter with nested hash :options is gone! below is the output of params in terminal & some of its error info:



> {"date_begin"=>"2018-10-01", "date_end"=>"2018-10-03"}
> ArgumentError:
wrong number of arguments (given 3, expected 2)


I search everywhere on internet and tried some of the solutions but it won`t help. I do know this error is caused by parameter missing. Could any one help me finger it out why hashes cannot be passed to controller`s params in my case?



note: I am using rails 5.1.6







ruby-on-rails rspec parameters controller






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 2:00

























asked Nov 7 at 11:17









weird honey

17010




17010








  • 1




    1) Change options={country_code: "US"} to options: {country_code: "US"} 2) Test.report receives only 2 arguments, you are passing 3
    – chumakoff
    Nov 7 at 11:37










  • @chumakoff after I change options to options: {country_code: "US"}.to_s and in controller eval(params[:options]) it worked fine. but this is not a really good way to solve the problem.
    – weird honey
    Nov 8 at 2:02










  • Are you kidding me? You just created another problem! It must be like this: params.permit(:date_begin, :date_end, options: [:country_code]) and without .to_s
    – chumakoff
    Nov 8 at 8:51










  • @chumakoff please see the answer below, method in comment was a temporary solution and now I replaced it with which in my answer.
    – weird honey
    Nov 8 at 10:30














  • 1




    1) Change options={country_code: "US"} to options: {country_code: "US"} 2) Test.report receives only 2 arguments, you are passing 3
    – chumakoff
    Nov 7 at 11:37










  • @chumakoff after I change options to options: {country_code: "US"}.to_s and in controller eval(params[:options]) it worked fine. but this is not a really good way to solve the problem.
    – weird honey
    Nov 8 at 2:02










  • Are you kidding me? You just created another problem! It must be like this: params.permit(:date_begin, :date_end, options: [:country_code]) and without .to_s
    – chumakoff
    Nov 8 at 8:51










  • @chumakoff please see the answer below, method in comment was a temporary solution and now I replaced it with which in my answer.
    – weird honey
    Nov 8 at 10:30








1




1




1) Change options={country_code: "US"} to options: {country_code: "US"} 2) Test.report receives only 2 arguments, you are passing 3
– chumakoff
Nov 7 at 11:37




1) Change options={country_code: "US"} to options: {country_code: "US"} 2) Test.report receives only 2 arguments, you are passing 3
– chumakoff
Nov 7 at 11:37












@chumakoff after I change options to options: {country_code: "US"}.to_s and in controller eval(params[:options]) it worked fine. but this is not a really good way to solve the problem.
– weird honey
Nov 8 at 2:02




@chumakoff after I change options to options: {country_code: "US"}.to_s and in controller eval(params[:options]) it worked fine. but this is not a really good way to solve the problem.
– weird honey
Nov 8 at 2:02












Are you kidding me? You just created another problem! It must be like this: params.permit(:date_begin, :date_end, options: [:country_code]) and without .to_s
– chumakoff
Nov 8 at 8:51




Are you kidding me? You just created another problem! It must be like this: params.permit(:date_begin, :date_end, options: [:country_code]) and without .to_s
– chumakoff
Nov 8 at 8:51












@chumakoff please see the answer below, method in comment was a temporary solution and now I replaced it with which in my answer.
– weird honey
Nov 8 at 10:30




@chumakoff please see the answer below, method in comment was a temporary solution and now I replaced it with which in my answer.
– weird honey
Nov 8 at 10:30












1 Answer
1






active

oldest

votes

















up vote
0
down vote













after searching for lots of solutions, i got one clear way to handle it. Strong parameters handling helped me a lot.



first, if you want to pass an array or hash, whatever the parameter is, only if it is a nested parameter, you should claim it inside the params.permit() statement, and add what key is allowed to pass in , because permit only allows scalar values to pass, and filter hashes and arrays by default if the specific parameter is not claimed. code as below:



private
def stat_params
params.permit(:date_begin, :date_end, options: [:country_code])
end


second, when using Rspec to test your controller, nested parameters will be passed as <ActionController::Parameters:xxxx>type, so you have to add to_unsafe_h statement to this specific parameter, as below:



def index
render json: Test.report(stat_params[:date_begin],
stat_params[:date_end],
stat_params[:options].to_unfsafe_h)
end


this works fine for me finally.






share|improve this answer





















    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',
    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%2f53188411%2frails-cannot-pass-nested-hash-to-controller-params%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    after searching for lots of solutions, i got one clear way to handle it. Strong parameters handling helped me a lot.



    first, if you want to pass an array or hash, whatever the parameter is, only if it is a nested parameter, you should claim it inside the params.permit() statement, and add what key is allowed to pass in , because permit only allows scalar values to pass, and filter hashes and arrays by default if the specific parameter is not claimed. code as below:



    private
    def stat_params
    params.permit(:date_begin, :date_end, options: [:country_code])
    end


    second, when using Rspec to test your controller, nested parameters will be passed as <ActionController::Parameters:xxxx>type, so you have to add to_unsafe_h statement to this specific parameter, as below:



    def index
    render json: Test.report(stat_params[:date_begin],
    stat_params[:date_end],
    stat_params[:options].to_unfsafe_h)
    end


    this works fine for me finally.






    share|improve this answer

























      up vote
      0
      down vote













      after searching for lots of solutions, i got one clear way to handle it. Strong parameters handling helped me a lot.



      first, if you want to pass an array or hash, whatever the parameter is, only if it is a nested parameter, you should claim it inside the params.permit() statement, and add what key is allowed to pass in , because permit only allows scalar values to pass, and filter hashes and arrays by default if the specific parameter is not claimed. code as below:



      private
      def stat_params
      params.permit(:date_begin, :date_end, options: [:country_code])
      end


      second, when using Rspec to test your controller, nested parameters will be passed as <ActionController::Parameters:xxxx>type, so you have to add to_unsafe_h statement to this specific parameter, as below:



      def index
      render json: Test.report(stat_params[:date_begin],
      stat_params[:date_end],
      stat_params[:options].to_unfsafe_h)
      end


      this works fine for me finally.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        after searching for lots of solutions, i got one clear way to handle it. Strong parameters handling helped me a lot.



        first, if you want to pass an array or hash, whatever the parameter is, only if it is a nested parameter, you should claim it inside the params.permit() statement, and add what key is allowed to pass in , because permit only allows scalar values to pass, and filter hashes and arrays by default if the specific parameter is not claimed. code as below:



        private
        def stat_params
        params.permit(:date_begin, :date_end, options: [:country_code])
        end


        second, when using Rspec to test your controller, nested parameters will be passed as <ActionController::Parameters:xxxx>type, so you have to add to_unsafe_h statement to this specific parameter, as below:



        def index
        render json: Test.report(stat_params[:date_begin],
        stat_params[:date_end],
        stat_params[:options].to_unfsafe_h)
        end


        this works fine for me finally.






        share|improve this answer












        after searching for lots of solutions, i got one clear way to handle it. Strong parameters handling helped me a lot.



        first, if you want to pass an array or hash, whatever the parameter is, only if it is a nested parameter, you should claim it inside the params.permit() statement, and add what key is allowed to pass in , because permit only allows scalar values to pass, and filter hashes and arrays by default if the specific parameter is not claimed. code as below:



        private
        def stat_params
        params.permit(:date_begin, :date_end, options: [:country_code])
        end


        second, when using Rspec to test your controller, nested parameters will be passed as <ActionController::Parameters:xxxx>type, so you have to add to_unsafe_h statement to this specific parameter, as below:



        def index
        render json: Test.report(stat_params[:date_begin],
        stat_params[:date_end],
        stat_params[:options].to_unfsafe_h)
        end


        this works fine for me finally.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 6:29









        weird honey

        17010




        17010






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53188411%2frails-cannot-pass-nested-hash-to-controller-params%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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini