私はRuby on Railsとスタックオーバーフローの初心者です。この質問に間違いがあった場合は、ごめんなさい...未定義のメソッドupdate for nil:NilClass rails 4.0.0
私のブロガープロジェクトの編集/更新を書こうとしています。これは私のコントローラです:
def edit
@post = Post.find params[:id]
end
def update
@post.update(params[:post].permit(:title, :summary, :content))
redirect_to posts_path
end
これが私の見解です:
<h1>Edit Page</h1>
<%= form_for @post do |f| %>
Title: <%= f.text_field :title %>
Summary: <%= f.text_area :summary %>
Content: <%= f.text_area :content %>
<%= f.submit "Update" %>
<% end %>
と私は、私はこのエラーを取得しておく任意の記事を更新したい:
NoMethodError in PostsController#update
undefined method `update' for nil:NilClass
任意の助けが理解されるであろう! :)
before_filterで@postを設定してください。 – Mohanraj
'before_filter'の[new syntax](http://stackoverflow.com/a/16519841/2909897)は' before_action'です。 – mbigras