Error 400 when commenting on ruby on rails ecommerce web app





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm actually making an ecommerce web app which has a User, Category, Book and Comment models. Everything is working nice, but when I try to comment in one of the book, it gives a 400 error. I really need you to help me out. https://github.com/felixpro/Book-app this is my repository.



This is my CommentsController



class CommentsController < ApplicationController

before_action :authenticate_user!

def create
book = Book.find(params[:comment][:book_id])
comment = book.comments.build(comment_params)
comment.user = current_user

if comment.save
redirect_to book_path(@book)
end
end

private

def comment_params
params.require(:comment).permit(:body)
end

end


This is the comment view partial,



<% if signed_in? %>
<div class="card bg-light new-comment">
<div class="card-body">
<p class="font-weight-bold">Deja tu comentario:</p>
<%= form_for @book.comments.build do |f| %>
<%= f.hidden_field :book_id, value: @book.id %>
<div class="form-group">
<%= f.text_area :body, rows: 4, class: "form-control" %>
</div>

<div class="text-right">
<%= f.submit "Comentar", class: "btn btn-primary" %>
</div>
<% end %>
</div>
</div>
<% else %>
<div class="card bg-light mt-5">
<div class="card-body">
<p class="card-text text-center lead"><%= link_to "Regístrate", new_user_registration_path %> o <%= link_to "Ingresa", new_user_session_path %> para comentar</p>
</div>
</div>
<% end %>


Here are the routes



Rails.application.routes.draw do
devise_for :users
root 'books#index'
resources :books

resources :comments, only: [:create]

end


The error say



This is a pictures showing the error message










share|improve this question

























  • did you have a route for book show page ?

    – wasipeer
    Jan 4 at 12:14











  • Please, add routes.rb and server logs for create action with full error backtrace

    – Vasilisa
    Jan 4 at 12:20











  • HI! Thanks for the fast answer, I edit my question and I added the routes.

    – Félix Pujols
    Jan 4 at 12:28











  • can you share the input and some more error details

    – wasipeer
    Jan 4 at 12:49











  • please commit your changes and push them on github i will have a look and let you know the issue

    – wasipeer
    Jan 4 at 12:59


















0















I'm actually making an ecommerce web app which has a User, Category, Book and Comment models. Everything is working nice, but when I try to comment in one of the book, it gives a 400 error. I really need you to help me out. https://github.com/felixpro/Book-app this is my repository.



This is my CommentsController



class CommentsController < ApplicationController

before_action :authenticate_user!

def create
book = Book.find(params[:comment][:book_id])
comment = book.comments.build(comment_params)
comment.user = current_user

if comment.save
redirect_to book_path(@book)
end
end

private

def comment_params
params.require(:comment).permit(:body)
end

end


This is the comment view partial,



<% if signed_in? %>
<div class="card bg-light new-comment">
<div class="card-body">
<p class="font-weight-bold">Deja tu comentario:</p>
<%= form_for @book.comments.build do |f| %>
<%= f.hidden_field :book_id, value: @book.id %>
<div class="form-group">
<%= f.text_area :body, rows: 4, class: "form-control" %>
</div>

<div class="text-right">
<%= f.submit "Comentar", class: "btn btn-primary" %>
</div>
<% end %>
</div>
</div>
<% else %>
<div class="card bg-light mt-5">
<div class="card-body">
<p class="card-text text-center lead"><%= link_to "Regístrate", new_user_registration_path %> o <%= link_to "Ingresa", new_user_session_path %> para comentar</p>
</div>
</div>
<% end %>


Here are the routes



Rails.application.routes.draw do
devise_for :users
root 'books#index'
resources :books

resources :comments, only: [:create]

end


The error say



This is a pictures showing the error message










share|improve this question

























  • did you have a route for book show page ?

    – wasipeer
    Jan 4 at 12:14











  • Please, add routes.rb and server logs for create action with full error backtrace

    – Vasilisa
    Jan 4 at 12:20











  • HI! Thanks for the fast answer, I edit my question and I added the routes.

    – Félix Pujols
    Jan 4 at 12:28











  • can you share the input and some more error details

    – wasipeer
    Jan 4 at 12:49











  • please commit your changes and push them on github i will have a look and let you know the issue

    – wasipeer
    Jan 4 at 12:59














0












0








0








I'm actually making an ecommerce web app which has a User, Category, Book and Comment models. Everything is working nice, but when I try to comment in one of the book, it gives a 400 error. I really need you to help me out. https://github.com/felixpro/Book-app this is my repository.



This is my CommentsController



class CommentsController < ApplicationController

before_action :authenticate_user!

def create
book = Book.find(params[:comment][:book_id])
comment = book.comments.build(comment_params)
comment.user = current_user

if comment.save
redirect_to book_path(@book)
end
end

private

def comment_params
params.require(:comment).permit(:body)
end

end


This is the comment view partial,



<% if signed_in? %>
<div class="card bg-light new-comment">
<div class="card-body">
<p class="font-weight-bold">Deja tu comentario:</p>
<%= form_for @book.comments.build do |f| %>
<%= f.hidden_field :book_id, value: @book.id %>
<div class="form-group">
<%= f.text_area :body, rows: 4, class: "form-control" %>
</div>

<div class="text-right">
<%= f.submit "Comentar", class: "btn btn-primary" %>
</div>
<% end %>
</div>
</div>
<% else %>
<div class="card bg-light mt-5">
<div class="card-body">
<p class="card-text text-center lead"><%= link_to "Regístrate", new_user_registration_path %> o <%= link_to "Ingresa", new_user_session_path %> para comentar</p>
</div>
</div>
<% end %>


Here are the routes



Rails.application.routes.draw do
devise_for :users
root 'books#index'
resources :books

resources :comments, only: [:create]

end


The error say



This is a pictures showing the error message










share|improve this question
















I'm actually making an ecommerce web app which has a User, Category, Book and Comment models. Everything is working nice, but when I try to comment in one of the book, it gives a 400 error. I really need you to help me out. https://github.com/felixpro/Book-app this is my repository.



This is my CommentsController



class CommentsController < ApplicationController

before_action :authenticate_user!

def create
book = Book.find(params[:comment][:book_id])
comment = book.comments.build(comment_params)
comment.user = current_user

if comment.save
redirect_to book_path(@book)
end
end

private

def comment_params
params.require(:comment).permit(:body)
end

end


This is the comment view partial,



<% if signed_in? %>
<div class="card bg-light new-comment">
<div class="card-body">
<p class="font-weight-bold">Deja tu comentario:</p>
<%= form_for @book.comments.build do |f| %>
<%= f.hidden_field :book_id, value: @book.id %>
<div class="form-group">
<%= f.text_area :body, rows: 4, class: "form-control" %>
</div>

<div class="text-right">
<%= f.submit "Comentar", class: "btn btn-primary" %>
</div>
<% end %>
</div>
</div>
<% else %>
<div class="card bg-light mt-5">
<div class="card-body">
<p class="card-text text-center lead"><%= link_to "Regístrate", new_user_registration_path %> o <%= link_to "Ingresa", new_user_session_path %> para comentar</p>
</div>
</div>
<% end %>


Here are the routes



Rails.application.routes.draw do
devise_for :users
root 'books#index'
resources :books

resources :comments, only: [:create]

end


The error say



This is a pictures showing the error message







ruby-on-rails ruby






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 12:55







Félix Pujols

















asked Jan 4 at 12:08









Félix PujolsFélix Pujols

33




33













  • did you have a route for book show page ?

    – wasipeer
    Jan 4 at 12:14











  • Please, add routes.rb and server logs for create action with full error backtrace

    – Vasilisa
    Jan 4 at 12:20











  • HI! Thanks for the fast answer, I edit my question and I added the routes.

    – Félix Pujols
    Jan 4 at 12:28











  • can you share the input and some more error details

    – wasipeer
    Jan 4 at 12:49











  • please commit your changes and push them on github i will have a look and let you know the issue

    – wasipeer
    Jan 4 at 12:59



















  • did you have a route for book show page ?

    – wasipeer
    Jan 4 at 12:14











  • Please, add routes.rb and server logs for create action with full error backtrace

    – Vasilisa
    Jan 4 at 12:20











  • HI! Thanks for the fast answer, I edit my question and I added the routes.

    – Félix Pujols
    Jan 4 at 12:28











  • can you share the input and some more error details

    – wasipeer
    Jan 4 at 12:49











  • please commit your changes and push them on github i will have a look and let you know the issue

    – wasipeer
    Jan 4 at 12:59

















did you have a route for book show page ?

– wasipeer
Jan 4 at 12:14





did you have a route for book show page ?

– wasipeer
Jan 4 at 12:14













Please, add routes.rb and server logs for create action with full error backtrace

– Vasilisa
Jan 4 at 12:20





Please, add routes.rb and server logs for create action with full error backtrace

– Vasilisa
Jan 4 at 12:20













HI! Thanks for the fast answer, I edit my question and I added the routes.

– Félix Pujols
Jan 4 at 12:28





HI! Thanks for the fast answer, I edit my question and I added the routes.

– Félix Pujols
Jan 4 at 12:28













can you share the input and some more error details

– wasipeer
Jan 4 at 12:49





can you share the input and some more error details

– wasipeer
Jan 4 at 12:49













please commit your changes and push them on github i will have a look and let you know the issue

– wasipeer
Jan 4 at 12:59





please commit your changes and push them on github i will have a look and let you know the issue

– wasipeer
Jan 4 at 12:59












2 Answers
2






active

oldest

votes


















2














The error you mentioned is linked to the fact that you have a special invisible character (non-breaking space) at line 9 and 14 in your CommentsController. This is why you get the



NameError (undefined local variable or method `​' for ...)


This often happens when you hit an additional key at the same time you hit the space bar (cmd + space bar on MacOS). Delete those empty lines and type the enter key again to clear the character.



Then the other answer is right, you'll have have to update your book variable name.






share|improve this answer


























  • Nice catch. Couldn't possibly see that at a glance!

    – R. Hatherall
    Jan 4 at 13:24











  • Thanks! it takes some practice and some hours debugging to track those ones!

    – Vincent Rolea
    Jan 4 at 13:25











  • You were right! Thanks very much.

    – Félix Pujols
    Jan 4 at 13:34



















0














You have referred to @book when the variable is a local book. Use @ at the beginning of the line 6:




@book = Book.find(params[:comment][:book_id])







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%2f54038690%2ferror-400-when-commenting-on-ruby-on-rails-ecommerce-web-app%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    The error you mentioned is linked to the fact that you have a special invisible character (non-breaking space) at line 9 and 14 in your CommentsController. This is why you get the



    NameError (undefined local variable or method `​' for ...)


    This often happens when you hit an additional key at the same time you hit the space bar (cmd + space bar on MacOS). Delete those empty lines and type the enter key again to clear the character.



    Then the other answer is right, you'll have have to update your book variable name.






    share|improve this answer


























    • Nice catch. Couldn't possibly see that at a glance!

      – R. Hatherall
      Jan 4 at 13:24











    • Thanks! it takes some practice and some hours debugging to track those ones!

      – Vincent Rolea
      Jan 4 at 13:25











    • You were right! Thanks very much.

      – Félix Pujols
      Jan 4 at 13:34
















    2














    The error you mentioned is linked to the fact that you have a special invisible character (non-breaking space) at line 9 and 14 in your CommentsController. This is why you get the



    NameError (undefined local variable or method `​' for ...)


    This often happens when you hit an additional key at the same time you hit the space bar (cmd + space bar on MacOS). Delete those empty lines and type the enter key again to clear the character.



    Then the other answer is right, you'll have have to update your book variable name.






    share|improve this answer


























    • Nice catch. Couldn't possibly see that at a glance!

      – R. Hatherall
      Jan 4 at 13:24











    • Thanks! it takes some practice and some hours debugging to track those ones!

      – Vincent Rolea
      Jan 4 at 13:25











    • You were right! Thanks very much.

      – Félix Pujols
      Jan 4 at 13:34














    2












    2








    2







    The error you mentioned is linked to the fact that you have a special invisible character (non-breaking space) at line 9 and 14 in your CommentsController. This is why you get the



    NameError (undefined local variable or method `​' for ...)


    This often happens when you hit an additional key at the same time you hit the space bar (cmd + space bar on MacOS). Delete those empty lines and type the enter key again to clear the character.



    Then the other answer is right, you'll have have to update your book variable name.






    share|improve this answer















    The error you mentioned is linked to the fact that you have a special invisible character (non-breaking space) at line 9 and 14 in your CommentsController. This is why you get the



    NameError (undefined local variable or method `​' for ...)


    This often happens when you hit an additional key at the same time you hit the space bar (cmd + space bar on MacOS). Delete those empty lines and type the enter key again to clear the character.



    Then the other answer is right, you'll have have to update your book variable name.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 4 at 14:15

























    answered Jan 4 at 13:22









    Vincent RoleaVincent Rolea

    595316




    595316













    • Nice catch. Couldn't possibly see that at a glance!

      – R. Hatherall
      Jan 4 at 13:24











    • Thanks! it takes some practice and some hours debugging to track those ones!

      – Vincent Rolea
      Jan 4 at 13:25











    • You were right! Thanks very much.

      – Félix Pujols
      Jan 4 at 13:34



















    • Nice catch. Couldn't possibly see that at a glance!

      – R. Hatherall
      Jan 4 at 13:24











    • Thanks! it takes some practice and some hours debugging to track those ones!

      – Vincent Rolea
      Jan 4 at 13:25











    • You were right! Thanks very much.

      – Félix Pujols
      Jan 4 at 13:34

















    Nice catch. Couldn't possibly see that at a glance!

    – R. Hatherall
    Jan 4 at 13:24





    Nice catch. Couldn't possibly see that at a glance!

    – R. Hatherall
    Jan 4 at 13:24













    Thanks! it takes some practice and some hours debugging to track those ones!

    – Vincent Rolea
    Jan 4 at 13:25





    Thanks! it takes some practice and some hours debugging to track those ones!

    – Vincent Rolea
    Jan 4 at 13:25













    You were right! Thanks very much.

    – Félix Pujols
    Jan 4 at 13:34





    You were right! Thanks very much.

    – Félix Pujols
    Jan 4 at 13:34













    0














    You have referred to @book when the variable is a local book. Use @ at the beginning of the line 6:




    @book = Book.find(params[:comment][:book_id])







    share|improve this answer




























      0














      You have referred to @book when the variable is a local book. Use @ at the beginning of the line 6:




      @book = Book.find(params[:comment][:book_id])







      share|improve this answer


























        0












        0








        0







        You have referred to @book when the variable is a local book. Use @ at the beginning of the line 6:




        @book = Book.find(params[:comment][:book_id])







        share|improve this answer













        You have referred to @book when the variable is a local book. Use @ at the beginning of the line 6:




        @book = Book.find(params[:comment][:book_id])








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 4 at 13:18









        R. HatherallR. Hatherall

        831811




        831811






























            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%2f54038690%2ferror-400-when-commenting-on-ruby-on-rails-ecommerce-web-app%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

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas