Form_For "First Argument in Form Cannot Contain Nil or Be Empty" Error

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

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.

First argument in form cannot contain nil or be empty (rails 5)

It does not look like you have an edit method in your Articles controller.

def edit
@article = Article.find(params[:id])
end

Just so it is clear. The edit method is what is called with the GET route that shows the form. Update is the PATCH/PUT route that takes the form in and updates the record. So, the form is shown by the edit method via GET, and processed by the update method via PUT/PATCH.

Form_for First argument in form cannot contain nil or be empty error

Assuming you are rendering this from PostsController and using the conventional view name, your new method should create a new Post and assign it:

def new
@post = Post.new
end

You can use the class name (as @Yuriy suggested), but the conventional way is to instantiate a new object. That allows you to re-use the same form view for rendering errors after a save.

If you want to see how this normally looks, create a new Rails project and use the scaffold generator to create some sample code.

can't be resolved First argument in form cannot contain nil or be empty

Try replacing

form_for([@micropost, @comment])

with

form_for([@micropost, Comment.new])

Getting First argument in form cannot contain nil or be empty error

If I'm correct you are rendering the partial in index.html.erb, if so change the index action in the controller to below

def index
@products = Product.all
@product = Product.new
end

Rails 4: Receiving the following error: First argument in form cannot contain nil or be empty

This error means that the first argument to form_for is a nil value (in this case @book). Most of the times I've seen this, it's due to malformed controller actions, but that doesn't look to be the case here. From what I can tell, it's one of two things:

  1. You're trying to edit a Book that doesn't exist. Do a .nil? check on it before deciding to render the form, and render an error message (or redirect) instead.

  2. Your routes are broken, and the edit action is not rendering the edit view. This is most likely not the case.

EDIT:

After updating with your template for show, this looks like your problem:

<%= link_to "Edit", edit_path, class: "btn btn-primary" %>

I see two problems with this (though I'll need to see the output of rake routes to verify). Firstly, you need to pass an argument to the edit path (without them, where would your params come from?). Secondly, the default route for this would be edit_book_path.

Try this:

<%= link_to "Edit", edit_book_path(book), class: "btn btn-primary" %>

Form_for “First argument in form cannot contain nil or be empty” error

Here is the code below how your code should be :

new form :

<%= form_for :weight do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_field :weight, placeholder: "Enter weight" %>
<%= hidden_field_tag :id, :value => current_user.id %>
</div>
<div class="field">
<%= f.hidden_field :date, :value => Time.now %>
</div>
<%= f.submit "Add weight", class: "btn" %>
<% end %>

edit form :

<%= form_for @weight do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_field :weight, placeholder: "Add weight" %>
</div>
<div class="field">
<%= f.hidden_field :date, :value => Time.now %>
<%= hidden_field_tag :id, :value => current_user.id %>
</div>
<%= f.submit "Update Weight", class: "btn btn-primary" %>
<% end %>

In this case by adding id field as hidden field to your forms you will not get user.id nil error for both create & update methods and your before_action filter will work fine.
OR
you can simply remove update & create methods from the before_action filter list like :

before_action :correct_user, only: [:destroy]

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

The form is on the edit action, not update.

Thus, you need to add the edit action, from which the @user instance variable will be taken:

def edit
@user = User.find(params[:id])
end


Related Topics



Leave a reply



Submit