Skip to main content
Ask A Question
Notifications
You’re not receiving notifications from this thread.
Subscribe

Forum_posts edit coming up as nil

General • Asked by Erick Sitter

ActionView::Template::Error (First argument in form cannot contain nil or be empty):
1: <%= form_for [@forum_thread, @forum_post] do |f| %>
2:

Reply to thread


3:

4: <%= f.text_area :body, placeholder: "Add a comment", rows: 10, class: "form-control"$
app/views/forum_posts/form.html.erb:1:in `_app_views_forum_postsform_html_erb_3822141534$
app/views/forum_threads/forum_posts/edit.html.erb:2:in `_app_views_forum_threads_forum_posts
$

Forum Posts Controller

class ForumThreads::ForumPostsController < ApplicationController
before_action :authenticate_user!
before_action :set_forum_thread
def create
@forum_post = @forum_thread.forum_posts.new forum_post_params
@forum_post.user = current_user

if @forum_post.save
  redirect_to forum_thread_path(@forum_thread, anchor: "forum_post_#{@forum_post.id}"), not$
else
  redirect_to @forum_thread, alert: "Unable to save your post"
end

end

def edit
end

def update
if @forum_post.update(forum_post_params)
redirect_to @forum_post
else
render 'edit'
end
end

private

def set_forum_thread
  @forum_thread = ForumThread.find(params[:forum_thread_id])
end

def forum_post_params
  params.require(:forum_post).permit(:body)
end

end


So ActionView::Template::Error (First argument in form cannot contain nil or be empty) means that when you call your form_for, one of the variables you passed in was nil. That means either forum_thread or forum_post in this was nil <%= form_for [@forum_thread, @forum_post] do |f| %>

It looks like you've got the before_action to set the forum_thread variable, but none on the edit action (or update) to set the forum_post. So most likely the forum_post is nil causing it to throw that error. Set that variable and you should be all set!


Thanks, got it!

But had to move it to edit.


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 30,005+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.