First argument in form cannot contain nil or be empty - Rails 4

前端 未结 7 1733
刺人心
刺人心 2020-12-04 19:24

I receive this error with my contact form in rails:

First argument in form cannot contain nil or be empty

View:

相关标签:
7条回答
  • 2020-12-04 20:03

    The error message is telling you that you can't have the following:

    <%= form_for nil do |f| %>
    <%= form_for [] do |f| %>
    

    My guess here is that your @contact is set to nil and that it doesn't come from your Contact#new action.

    FYI it would simply work if you do this:

    <%= form_for Contact.new do |f| %>
    

    Though it is not recommended.

    You need to check that the view containing your form is actually rendered by the new action of your ContactsController.

    0 讨论(0)
  • 2020-12-04 20:03

    It can depend on where your new or create action is defined. I had the same issue, turned out i had to put the @Object = object.new command inside the page method.

    For example..

    class PagesController < ApplicationController
    
    def home
        @applicant = Applicant.new
    end
    
    def about
    end
    
    def homeowner
    end
    

    end

    I before defined a "new" and "create" method and had the action in them which is usual and i do in other instances. Still don't know why.

    0 讨论(0)
  • 2020-12-04 20:04

    If in fact your instance variable is supposed to be nil, you could use :contact instead of @contact and provide a path.

    In your case, the code should be,

    <%= form_for( :contact , :html => {:class => "form-horizontal", :role => "form"}, url: login_path) do |form| %>
    

    The login_path is just an example and can be replaced as seen fit.

    0 讨论(0)
  • 2020-12-04 20:13

    I know this has an accepted answer, but that didn't solved my same issue(i didn't wanted to use new in my view) and i intend to post what solved mine.

    This is always a typo, you can check your routes with $rake routes if routes are ok, than its some kind of type somewhere else.

    In my case i was using sublime editor, and i had an extra def above the def new in my controller, i don't know how it got there, but it was there...Removing it solved my issue after about half hour of nail bitting... There can be any kind of typo causing this issue. This error just means that the new method is not being accessed, if it was being accessed it would have initialized the variable.

    I repeat, always a typo somewhere.

    Hope this helps anyone.

    0 讨论(0)
  • 2020-12-04 20:13

    I had the same problem. I have a Comment model, a CommentsController, a 'show' action and a 'show' view for the comment. I also have a '_show' partial rendered inside the 'show' view with following local:

    :locals => {:comment => @comment}
    

    and inside the '_show' partial I have a

    <%= form_for(comment, :remote => true) do |f| %>
    

    which troughs the "First argument in form cannot contain nil or be empty". I only could solve the problem by renaming the local:

    :locals => {:new_comment => @comment}
    

    and within the partial:

    <%= form_for(new_comment, :remote => true) do |f| %>
    

    It seems using the same name for the Model and the partial local caused some conflict for me.

    0 讨论(0)
  • 2020-12-04 20:15

    I had the same problem but it was easy to repair. In your #example posts_controler.rb add the same line to find post like in edit. In my application it's

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

    It doesn't work here becase I added it but did not save my changes ;).

    0 讨论(0)
提交回复
热议问题