mercredi 11 novembre 2015

Nested def edit undefined method error

I have three models with relationships: Forum, Topic and Post. I want to edit Post.

This is Post_controller:

def update
  @forum = Forum.find params[:forum_id]
  @topic = Topic.find params[:topic_id]
  @post = @topic.posts.find(params[:id])
  if @post.update(post_params)
    redirect_to forum_topic_path(@forum.id, @topic.id)
  end
end

def edit
  @forum = Forum.find params[:forum_id]
  @topic = Topic.find params[:topic_id]
  @post = Post.find(params[:id])
end

This is edit.html.slim:

= simple_form_for ([@forum, @topics, @topics.posts.build]) do |f|
  div.new_message
    p 
        b Ответ в тему  
    = f.label 'Текст сообщения:'
    = f.text_area :content,rows: '15', cols: '82' 
    div.forum_button
      = f.submit 'Изменить' 

This is the error:

NoMethodError in Posts#edit

undefined method `posts' for nil:NilClass

Aucun commentaire:

Enregistrer un commentaire