Question

I have two forms to create a new image. I want them both to go to the new_image_path but with the @image variable.

I get this error for the form_for lines:

First argument in form cannot contain nil or be empty

One form:

<%= form_for @image, url: new_sm_image_path(@image) do |f| %>

The other:

<%= form_for @image, url: new_lrg_image_path(@image) do |f| %>

My Routes:

get "/sm_image", to: "images#new_sm", as: "new_sm_image"
get "/lrg_image", to: "images#new_lrg", as: "new_lrg_image"

My index.html.erb has two buttons:

<%= link_to 'sm_image', id: "new_button" do %>
<%= link_to 'lrg_image', id: "new_button" do %>
Was it helpful?

Solution

Set @image instance variable in your Controller action's which are rendering these forms. Currently @image is nil, so you are getting the error as First argument in form cannot contain nil or be empty.

As per the comment,

In my index.html.erb I have two buttons that link_to the get routes I posted

you would need to set @image instance variable in two actions : new_sm and new_lrg as these are the actions rendering your views.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top