Rails parameters contain backslashes












1















I am currently working with button_tag to create a remote styled answer submission quiz. When pressing this button, instead of posting the new record, it is throwing an error.



ActiveRecord::RecordNotFound (Couldn't find Answer without an ID):



When looking at the server logs I see it is trying to work with these params when trying to post Parameters: {"{"answer_id":59}"=>nil, "id"=>"15"}



What I am looking, or expecting to see is this.
Parameters: {"answer_id"=>"59", "id"=>"15"}



Here is the button_tag I am using.



<% @question.answers.each do |answer| %>
<%= button_tag "#{answer.answer.titleize}", class: 'btn btn-block btn-lg btn-primary', data: {
remote: true,
method: :post,
url: answer_question_path(@question),
params: { answer_id: answer.id }
} %>
<% end %>


Here is my response controller which is responsible for submitting the POST request.



class ResponsesController < ApplicationController
def answer
question = Question.find(params[:id])
answer = question.answers.find(params[:answer_id])
response = question.responses.find_or_initialize_by(user: current_user)

if response.update(answer: answer)
head :ok
else
puts 'Something went wrong chief'
end
end

private

def responses_params
params.require(:response).permit(:user_id, :question_id, :answer_id)
end
end


I have tried using to_json on the parameter with no success and have not been able to find any solution elsewhere on SO or other forums. Any ideas?










share|improve this question


















  • 1





    Have you tried using button_to instead of button_tag? That first method has more control over remote without needing to hack around with data.

    – tadman
    Dec 28 '18 at 21:16











  • That's embarrassing. I tried earlier with no success but that seems to have worked now. Thank you.

    – Trenton Tyler
    Dec 28 '18 at 21:27











  • No shame in succeeding! If you've got a working solution using that approach showing the adapted code as a self-answer would be educational to others.

    – tadman
    Dec 28 '18 at 22:49
















1















I am currently working with button_tag to create a remote styled answer submission quiz. When pressing this button, instead of posting the new record, it is throwing an error.



ActiveRecord::RecordNotFound (Couldn't find Answer without an ID):



When looking at the server logs I see it is trying to work with these params when trying to post Parameters: {"{"answer_id":59}"=>nil, "id"=>"15"}



What I am looking, or expecting to see is this.
Parameters: {"answer_id"=>"59", "id"=>"15"}



Here is the button_tag I am using.



<% @question.answers.each do |answer| %>
<%= button_tag "#{answer.answer.titleize}", class: 'btn btn-block btn-lg btn-primary', data: {
remote: true,
method: :post,
url: answer_question_path(@question),
params: { answer_id: answer.id }
} %>
<% end %>


Here is my response controller which is responsible for submitting the POST request.



class ResponsesController < ApplicationController
def answer
question = Question.find(params[:id])
answer = question.answers.find(params[:answer_id])
response = question.responses.find_or_initialize_by(user: current_user)

if response.update(answer: answer)
head :ok
else
puts 'Something went wrong chief'
end
end

private

def responses_params
params.require(:response).permit(:user_id, :question_id, :answer_id)
end
end


I have tried using to_json on the parameter with no success and have not been able to find any solution elsewhere on SO or other forums. Any ideas?










share|improve this question


















  • 1





    Have you tried using button_to instead of button_tag? That first method has more control over remote without needing to hack around with data.

    – tadman
    Dec 28 '18 at 21:16











  • That's embarrassing. I tried earlier with no success but that seems to have worked now. Thank you.

    – Trenton Tyler
    Dec 28 '18 at 21:27











  • No shame in succeeding! If you've got a working solution using that approach showing the adapted code as a self-answer would be educational to others.

    – tadman
    Dec 28 '18 at 22:49














1












1








1








I am currently working with button_tag to create a remote styled answer submission quiz. When pressing this button, instead of posting the new record, it is throwing an error.



ActiveRecord::RecordNotFound (Couldn't find Answer without an ID):



When looking at the server logs I see it is trying to work with these params when trying to post Parameters: {"{"answer_id":59}"=>nil, "id"=>"15"}



What I am looking, or expecting to see is this.
Parameters: {"answer_id"=>"59", "id"=>"15"}



Here is the button_tag I am using.



<% @question.answers.each do |answer| %>
<%= button_tag "#{answer.answer.titleize}", class: 'btn btn-block btn-lg btn-primary', data: {
remote: true,
method: :post,
url: answer_question_path(@question),
params: { answer_id: answer.id }
} %>
<% end %>


Here is my response controller which is responsible for submitting the POST request.



class ResponsesController < ApplicationController
def answer
question = Question.find(params[:id])
answer = question.answers.find(params[:answer_id])
response = question.responses.find_or_initialize_by(user: current_user)

if response.update(answer: answer)
head :ok
else
puts 'Something went wrong chief'
end
end

private

def responses_params
params.require(:response).permit(:user_id, :question_id, :answer_id)
end
end


I have tried using to_json on the parameter with no success and have not been able to find any solution elsewhere on SO or other forums. Any ideas?










share|improve this question














I am currently working with button_tag to create a remote styled answer submission quiz. When pressing this button, instead of posting the new record, it is throwing an error.



ActiveRecord::RecordNotFound (Couldn't find Answer without an ID):



When looking at the server logs I see it is trying to work with these params when trying to post Parameters: {"{"answer_id":59}"=>nil, "id"=>"15"}



What I am looking, or expecting to see is this.
Parameters: {"answer_id"=>"59", "id"=>"15"}



Here is the button_tag I am using.



<% @question.answers.each do |answer| %>
<%= button_tag "#{answer.answer.titleize}", class: 'btn btn-block btn-lg btn-primary', data: {
remote: true,
method: :post,
url: answer_question_path(@question),
params: { answer_id: answer.id }
} %>
<% end %>


Here is my response controller which is responsible for submitting the POST request.



class ResponsesController < ApplicationController
def answer
question = Question.find(params[:id])
answer = question.answers.find(params[:answer_id])
response = question.responses.find_or_initialize_by(user: current_user)

if response.update(answer: answer)
head :ok
else
puts 'Something went wrong chief'
end
end

private

def responses_params
params.require(:response).permit(:user_id, :question_id, :answer_id)
end
end


I have tried using to_json on the parameter with no success and have not been able to find any solution elsewhere on SO or other forums. Any ideas?







ruby-on-rails ruby






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 28 '18 at 20:57









Trenton TylerTrenton Tyler

9001933




9001933








  • 1





    Have you tried using button_to instead of button_tag? That first method has more control over remote without needing to hack around with data.

    – tadman
    Dec 28 '18 at 21:16











  • That's embarrassing. I tried earlier with no success but that seems to have worked now. Thank you.

    – Trenton Tyler
    Dec 28 '18 at 21:27











  • No shame in succeeding! If you've got a working solution using that approach showing the adapted code as a self-answer would be educational to others.

    – tadman
    Dec 28 '18 at 22:49














  • 1





    Have you tried using button_to instead of button_tag? That first method has more control over remote without needing to hack around with data.

    – tadman
    Dec 28 '18 at 21:16











  • That's embarrassing. I tried earlier with no success but that seems to have worked now. Thank you.

    – Trenton Tyler
    Dec 28 '18 at 21:27











  • No shame in succeeding! If you've got a working solution using that approach showing the adapted code as a self-answer would be educational to others.

    – tadman
    Dec 28 '18 at 22:49








1




1





Have you tried using button_to instead of button_tag? That first method has more control over remote without needing to hack around with data.

– tadman
Dec 28 '18 at 21:16





Have you tried using button_to instead of button_tag? That first method has more control over remote without needing to hack around with data.

– tadman
Dec 28 '18 at 21:16













That's embarrassing. I tried earlier with no success but that seems to have worked now. Thank you.

– Trenton Tyler
Dec 28 '18 at 21:27





That's embarrassing. I tried earlier with no success but that seems to have worked now. Thank you.

– Trenton Tyler
Dec 28 '18 at 21:27













No shame in succeeding! If you've got a working solution using that approach showing the adapted code as a self-answer would be educational to others.

– tadman
Dec 28 '18 at 22:49





No shame in succeeding! If you've got a working solution using that approach showing the adapted code as a self-answer would be educational to others.

– tadman
Dec 28 '18 at 22:49












1 Answer
1






active

oldest

votes


















0














This seems to be an issue with button_tag in the feature I am using it for.



button_tag creates a button element that defines a submit button, resetbutton or a generic button which can be used in JavaScript. button_tag is also an action view helper but is defined as a FormTagHelper.



button_to generates a form containing a single button that submits to the URL created by the set of options. button_to is a UrlHelper while button_tag is a ViewHelper.



Below is the button_tag code I was using which was creating the issue described above. Using button_tag fixed my parameters issue and also looks a bit cleaner. I hope this helps anybody else having issues with button_tag in the future.



<%= button_tag "#{answer.answer.titleize}", class: 'btn btn-block btn-lg btn-primary', data: {
remote: true,
method: :post,
url: answer_question_path(@question),
params: { answer_id: answer.id }
} %>


<%= button_to "#{answer.answer.titleize}",
answer_question_path(@question),
class: 'btn btn-block btn-lg btn-primary',
params: { answer_id: answer.id },
remote: true %>





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',
    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%2f53964215%2frails-parameters-contain-backslashes%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









    0














    This seems to be an issue with button_tag in the feature I am using it for.



    button_tag creates a button element that defines a submit button, resetbutton or a generic button which can be used in JavaScript. button_tag is also an action view helper but is defined as a FormTagHelper.



    button_to generates a form containing a single button that submits to the URL created by the set of options. button_to is a UrlHelper while button_tag is a ViewHelper.



    Below is the button_tag code I was using which was creating the issue described above. Using button_tag fixed my parameters issue and also looks a bit cleaner. I hope this helps anybody else having issues with button_tag in the future.



    <%= button_tag "#{answer.answer.titleize}", class: 'btn btn-block btn-lg btn-primary', data: {
    remote: true,
    method: :post,
    url: answer_question_path(@question),
    params: { answer_id: answer.id }
    } %>


    <%= button_to "#{answer.answer.titleize}",
    answer_question_path(@question),
    class: 'btn btn-block btn-lg btn-primary',
    params: { answer_id: answer.id },
    remote: true %>





    share|improve this answer




























      0














      This seems to be an issue with button_tag in the feature I am using it for.



      button_tag creates a button element that defines a submit button, resetbutton or a generic button which can be used in JavaScript. button_tag is also an action view helper but is defined as a FormTagHelper.



      button_to generates a form containing a single button that submits to the URL created by the set of options. button_to is a UrlHelper while button_tag is a ViewHelper.



      Below is the button_tag code I was using which was creating the issue described above. Using button_tag fixed my parameters issue and also looks a bit cleaner. I hope this helps anybody else having issues with button_tag in the future.



      <%= button_tag "#{answer.answer.titleize}", class: 'btn btn-block btn-lg btn-primary', data: {
      remote: true,
      method: :post,
      url: answer_question_path(@question),
      params: { answer_id: answer.id }
      } %>


      <%= button_to "#{answer.answer.titleize}",
      answer_question_path(@question),
      class: 'btn btn-block btn-lg btn-primary',
      params: { answer_id: answer.id },
      remote: true %>





      share|improve this answer


























        0












        0








        0







        This seems to be an issue with button_tag in the feature I am using it for.



        button_tag creates a button element that defines a submit button, resetbutton or a generic button which can be used in JavaScript. button_tag is also an action view helper but is defined as a FormTagHelper.



        button_to generates a form containing a single button that submits to the URL created by the set of options. button_to is a UrlHelper while button_tag is a ViewHelper.



        Below is the button_tag code I was using which was creating the issue described above. Using button_tag fixed my parameters issue and also looks a bit cleaner. I hope this helps anybody else having issues with button_tag in the future.



        <%= button_tag "#{answer.answer.titleize}", class: 'btn btn-block btn-lg btn-primary', data: {
        remote: true,
        method: :post,
        url: answer_question_path(@question),
        params: { answer_id: answer.id }
        } %>


        <%= button_to "#{answer.answer.titleize}",
        answer_question_path(@question),
        class: 'btn btn-block btn-lg btn-primary',
        params: { answer_id: answer.id },
        remote: true %>





        share|improve this answer













        This seems to be an issue with button_tag in the feature I am using it for.



        button_tag creates a button element that defines a submit button, resetbutton or a generic button which can be used in JavaScript. button_tag is also an action view helper but is defined as a FormTagHelper.



        button_to generates a form containing a single button that submits to the URL created by the set of options. button_to is a UrlHelper while button_tag is a ViewHelper.



        Below is the button_tag code I was using which was creating the issue described above. Using button_tag fixed my parameters issue and also looks a bit cleaner. I hope this helps anybody else having issues with button_tag in the future.



        <%= button_tag "#{answer.answer.titleize}", class: 'btn btn-block btn-lg btn-primary', data: {
        remote: true,
        method: :post,
        url: answer_question_path(@question),
        params: { answer_id: answer.id }
        } %>


        <%= button_to "#{answer.answer.titleize}",
        answer_question_path(@question),
        class: 'btn btn-block btn-lg btn-primary',
        params: { answer_id: answer.id },
        remote: true %>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 30 '18 at 19:57









        Trenton TylerTrenton Tyler

        9001933




        9001933






























            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%2f53964215%2frails-parameters-contain-backslashes%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







            Popular posts from this blog

            Mossoró

            Can't read property showImagePicker of undefined in react native iOS

            Pushsharp Apns notification error: 'InvalidToken'