First argument in form cannot contain nil or be empty Hartl's Rails 4 Tutorial

后端 未结 3 636
醉酒成梦
醉酒成梦 2021-01-14 16:13

I\'m working through Michael Hartl\'s Rails tutorial, and I\'m running into an issue in section 7.3.3. I receive this error message:

ArgumentError in Users#         


        
相关标签:
3条回答
  • 2021-01-14 16:45

    In your new.html.erb you have specified form_for(@user) that means you need to have some value in instance variable @user before calling the new action.

    You can do it in two ways :

    one way is define an action new in your controller which would be called before rendering your new.html.erb layout. For eg:

    def new
      @user = User.new
    end
    

    other way is that in your form itself you could specify something like

    <%= form_for(User.new) do |f| %>
    

    Defining it in your new action is more standard way of doing it.

    0 讨论(0)
  • 2021-01-14 16:48

    I dont see a new action on you controller.

    def new
      @user = User.new
    end
    

    I maybe made indirectly, but it couldn't hurt defining it yourself. From you title that may be why the @user variable is nil, it wasn't defined.

    0 讨论(0)
  • 2021-01-14 16:56

    The order of actions in controller file matters. I had the same error until I figure out the problem on order of action methods. Example. Controller file

    def edit
      @post = Post.find(params[:id])
    end
    
    def update
    
    end
    
    private
    def post_params
        params.require(:post).permit(:title, :body)
    end
    

    I was working on edit action and was getting same error as you. The problem was I have written the edit action under private action. once I changed the order of this actions. It worked like a charm.

    What I was trying to achieve ? I was trying to take the values of post instance variable and use it into html form.

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