1

I'm getting an undefined method 'model_name' error with simple_form when I want to edit a basic post on Rails 5.

show.html.haml:

= link_to "Edit", edit_post_path(@post)

edit.html.haml:

= simple_form_for @post do |f|
    = f.input :title
    = f.input :link
    = f.input :description

    = f.button :submit

posts_controller.rb:

def show
  @post = Post.find(params[:id])
end

def edit
end

def update
  if @post.update(post_params)
    redirect_to @post
  else
    render 'edit'
  end
end

Any idea?

6
  • do you have edit action in controller? Commented Nov 17, 2016 at 14:17
  • yes, see I just edit my post Commented Nov 17, 2016 at 14:21
  • add @post = Post.find(params[:id]) to edit action Commented Nov 17, 2016 at 14:21
  • thx it's working! Commented Nov 17, 2016 at 14:23
  • This would have been far more obvious, had you posted the full error message -- Which, presumably, said "...for nil:NilClass", implying that your @post variable was nil. Commented Nov 17, 2016 at 15:28

1 Answer 1

0
def edit
  @post = Post.find(params[:id]) # <=========
end
Sign up to request clarification or add additional context in comments.

Comments

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.