New post - First argument in form cannot contain nil or be empty











up vote
0
down vote

favorite












This is error I'm getting when I click on "New Post:




First argument in form cannot contain nil or be empty




My PostController:



class PostsController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]

def index
@posts = Post.all
end

def show

end

def edit

end

def update
@post.update(post_params)
redirect_to posts_path
end

def new
if user_signed_in?
@post = Post.new
else
flash[:alert] = ":("
end
end

def create
@post = Post.new(post_params)
@post.user_id = current_user.id
@post.save
redirect_to posts_path
end

def destroy
if user_signed_in?
@post.destroy!
else
flash[:alert] = ":("
end
redirect_to posts_path
end


private
def post_params
params.require(:post).permit(:body)
end

def set_user
@post = Post.find(params[:id])
end


end



and posts#index:



<% @posts.each do |post|  %>
<%= post.body %>
<%= link_to 'Show', post %>
<%= link_to 'Edit', edit_post_path(post) %>
<%= link_to 'Delete', post, method: :delete %>
<br>









_form.html.erb



<%= form_for(@post) do |f| %>

<div class="field">
<%= f.label :body %><br>
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>









share|improve this question




























    up vote
    0
    down vote

    favorite












    This is error I'm getting when I click on "New Post:




    First argument in form cannot contain nil or be empty




    My PostController:



    class PostsController < ApplicationController
    before_action :set_user, only: [:show, :edit, :update, :destroy]

    def index
    @posts = Post.all
    end

    def show

    end

    def edit

    end

    def update
    @post.update(post_params)
    redirect_to posts_path
    end

    def new
    if user_signed_in?
    @post = Post.new
    else
    flash[:alert] = ":("
    end
    end

    def create
    @post = Post.new(post_params)
    @post.user_id = current_user.id
    @post.save
    redirect_to posts_path
    end

    def destroy
    if user_signed_in?
    @post.destroy!
    else
    flash[:alert] = ":("
    end
    redirect_to posts_path
    end


    private
    def post_params
    params.require(:post).permit(:body)
    end

    def set_user
    @post = Post.find(params[:id])
    end


    end



    and posts#index:



    <% @posts.each do |post|  %>
    <%= post.body %>
    <%= link_to 'Show', post %>
    <%= link_to 'Edit', edit_post_path(post) %>
    <%= link_to 'Delete', post, method: :delete %>
    <br>









    _form.html.erb



    <%= form_for(@post) do |f| %>

    <div class="field">
    <%= f.label :body %><br>
    <%= f.text_area :body %>
    </div>
    <div class="actions">
    <%= f.submit %>
    </div>
    <% end %>









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      This is error I'm getting when I click on "New Post:




      First argument in form cannot contain nil or be empty




      My PostController:



      class PostsController < ApplicationController
      before_action :set_user, only: [:show, :edit, :update, :destroy]

      def index
      @posts = Post.all
      end

      def show

      end

      def edit

      end

      def update
      @post.update(post_params)
      redirect_to posts_path
      end

      def new
      if user_signed_in?
      @post = Post.new
      else
      flash[:alert] = ":("
      end
      end

      def create
      @post = Post.new(post_params)
      @post.user_id = current_user.id
      @post.save
      redirect_to posts_path
      end

      def destroy
      if user_signed_in?
      @post.destroy!
      else
      flash[:alert] = ":("
      end
      redirect_to posts_path
      end


      private
      def post_params
      params.require(:post).permit(:body)
      end

      def set_user
      @post = Post.find(params[:id])
      end


      end



      and posts#index:



      <% @posts.each do |post|  %>
      <%= post.body %>
      <%= link_to 'Show', post %>
      <%= link_to 'Edit', edit_post_path(post) %>
      <%= link_to 'Delete', post, method: :delete %>
      <br>









      _form.html.erb



      <%= form_for(@post) do |f| %>

      <div class="field">
      <%= f.label :body %><br>
      <%= f.text_area :body %>
      </div>
      <div class="actions">
      <%= f.submit %>
      </div>
      <% end %>









      share|improve this question















      This is error I'm getting when I click on "New Post:




      First argument in form cannot contain nil or be empty




      My PostController:



      class PostsController < ApplicationController
      before_action :set_user, only: [:show, :edit, :update, :destroy]

      def index
      @posts = Post.all
      end

      def show

      end

      def edit

      end

      def update
      @post.update(post_params)
      redirect_to posts_path
      end

      def new
      if user_signed_in?
      @post = Post.new
      else
      flash[:alert] = ":("
      end
      end

      def create
      @post = Post.new(post_params)
      @post.user_id = current_user.id
      @post.save
      redirect_to posts_path
      end

      def destroy
      if user_signed_in?
      @post.destroy!
      else
      flash[:alert] = ":("
      end
      redirect_to posts_path
      end


      private
      def post_params
      params.require(:post).permit(:body)
      end

      def set_user
      @post = Post.find(params[:id])
      end


      end



      and posts#index:



      <% @posts.each do |post|  %>
      <%= post.body %>
      <%= link_to 'Show', post %>
      <%= link_to 'Edit', edit_post_path(post) %>
      <%= link_to 'Delete', post, method: :delete %>
      <br>









      _form.html.erb



      <%= form_for(@post) do |f| %>

      <div class="field">
      <%= f.label :body %><br>
      <%= f.text_area :body %>
      </div>
      <div class="actions">
      <%= f.submit %>
      </div>
      <% end %>






      ruby-on-rails ruby-on-rails-4






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 13:48









      Cœur

      17.1k9102140




      17.1k9102140










      asked May 30 '13 at 22:12









      fuskie

      356




      356
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          you should check your "new.html.erb" or "_form.html.erb" file. to see if the "form_for" method is correctly called.






          share|improve this answer





















          • error doesnt show up when im logged in
            – fuskie
            May 30 '13 at 22:31










          • please post all the error message you got.
            – Siwei Shen申思维
            May 31 '13 at 1:18


















          up vote
          0
          down vote













          If you are using devise, You can get this by adding a before action:



          before_action :authenticate_user!, only: [:new]


          and you new action as:



          def new
          @post = Post.new
          end





          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%2f16846676%2fnew-post-first-argument-in-form-cannot-contain-nil-or-be-empty%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








            up vote
            0
            down vote













            you should check your "new.html.erb" or "_form.html.erb" file. to see if the "form_for" method is correctly called.






            share|improve this answer





















            • error doesnt show up when im logged in
              – fuskie
              May 30 '13 at 22:31










            • please post all the error message you got.
              – Siwei Shen申思维
              May 31 '13 at 1:18















            up vote
            0
            down vote













            you should check your "new.html.erb" or "_form.html.erb" file. to see if the "form_for" method is correctly called.






            share|improve this answer





















            • error doesnt show up when im logged in
              – fuskie
              May 30 '13 at 22:31










            • please post all the error message you got.
              – Siwei Shen申思维
              May 31 '13 at 1:18













            up vote
            0
            down vote










            up vote
            0
            down vote









            you should check your "new.html.erb" or "_form.html.erb" file. to see if the "form_for" method is correctly called.






            share|improve this answer












            you should check your "new.html.erb" or "_form.html.erb" file. to see if the "form_for" method is correctly called.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered May 30 '13 at 22:17









            Siwei Shen申思维

            13.7k45366




            13.7k45366












            • error doesnt show up when im logged in
              – fuskie
              May 30 '13 at 22:31










            • please post all the error message you got.
              – Siwei Shen申思维
              May 31 '13 at 1:18


















            • error doesnt show up when im logged in
              – fuskie
              May 30 '13 at 22:31










            • please post all the error message you got.
              – Siwei Shen申思维
              May 31 '13 at 1:18
















            error doesnt show up when im logged in
            – fuskie
            May 30 '13 at 22:31




            error doesnt show up when im logged in
            – fuskie
            May 30 '13 at 22:31












            please post all the error message you got.
            – Siwei Shen申思维
            May 31 '13 at 1:18




            please post all the error message you got.
            – Siwei Shen申思维
            May 31 '13 at 1:18












            up vote
            0
            down vote













            If you are using devise, You can get this by adding a before action:



            before_action :authenticate_user!, only: [:new]


            and you new action as:



            def new
            @post = Post.new
            end





            share|improve this answer

























              up vote
              0
              down vote













              If you are using devise, You can get this by adding a before action:



              before_action :authenticate_user!, only: [:new]


              and you new action as:



              def new
              @post = Post.new
              end





              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                If you are using devise, You can get this by adding a before action:



                before_action :authenticate_user!, only: [:new]


                and you new action as:



                def new
                @post = Post.new
                end





                share|improve this answer












                If you are using devise, You can get this by adding a before action:



                before_action :authenticate_user!, only: [:new]


                and you new action as:



                def new
                @post = Post.new
                end






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jun 2 '16 at 10:12









                Atchyut Nagabhairava

                4492818




                4492818






























                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2f16846676%2fnew-post-first-argument-in-form-cannot-contain-nil-or-be-empty%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

                    List item for chat from Array inside array React Native

                    Thiostrepton

                    Caerphilly