0

This is pretty weird, I must just be missing something because I've done this a million times, and this is the first time I am seeing this.

Vanilla create controller action:

  def create
    @album = Album.new(album_params)

    respond_to do |format|
      if @album.save
        format.html { redirect_to @album, notice: 'Album was successfully created.' }
        format.json { render :show, status: :created, location: @album }
      else
        format.html { render :new }
        format.json { render json: @album.errors, status: :unprocessable_entity }
      end
    end
  end

  def album_params
    params.require(:album).permit(:title)
  end

And a vanilla view after a successful create of an Album:

<p id="notice"><%= notice %></p>

<p>
  <strong>Title:</strong>
  <%= @album.title %>
</p>

<%= link_to 'Edit', edit_album_path(@album) %> |
<%= link_to 'Back', albums_path %>

I checked through debugging that after I create the album, @album is correctly set up. I also set a second instance variable @foo = 100. But in any event when the view gets rendered, both instance variables are nil, and I get this error (see image, it was the easiest way)

enter image description here

1
  • Show your controller show action. Commented Oct 17, 2017 at 16:28

1 Answer 1

3

If you look at the request parameters section, you will see:

{ "controller" => "albums", "action" => "show", "id" => "20" }

The error is coming from rendering the show action, not the create action.

What is happening is that after you create the @album, it is doing a redirect_to the show action.

That show action is likely not defined and is not setting up the @album instance variable

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the friendly (see above) response to a dumb mistake :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.