-3

edit page: the first line of embedded ruby is giving me the trouble

Edit Post

<%= form_for @post do |f|%> (this line is the problem)
  <p> 
    <%= f.label :title %>
    <%= f.text_field :title%>
  </p>
  <p>            
    <%= f.label :content %>
    <%= f.text_area :content%>
  </p>
  <p>
    <%= f.submit "Update post"%>
  </p>
<% end %>

Look at the edit post, is this the problem? it seems like i was doing everthimg right and i just can't figure it out.

Post Controller

class PostsController < ApplicationController
   def index
      @posts = Post.all 
   end 

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

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

   def create  
      @post = Post.new(post_params)

      if @post.save
         redirect_to posts_path, :notice => "your post was saved"
      else
         render "new"
      end
   end 

private

   def post_params
      params.require(:post).permit(:title, :content)
   end

   def edit
      @post = Post.find(params[:id])
   end
Малъ Скрылевъ
  • 16,187
  • 5
  • 56
  • 69
Nate
  • 1
  • 3
  • Have you initialized `@post` in your controller action? Did you check if it is `nil` ? Did you check if the record actually exists in db? If you find answers to these questions, you will have the solution to your problem. – Santhosh Dec 13 '13 at 05:20
  • how can you check if it is nil? – Nate Dec 13 '13 at 05:33
  • Why is your edit action private? That is the problem. Move it above the private call. – Santhosh Dec 13 '13 at 05:38
  • It worked, thank you! I just started coding four days ago, so im a little bit behind lol but thanks for the help bud! – Nate Dec 13 '13 at 05:41

1 Answers1

0

The edit action is not visible as it is private and is not executed. Hence @post is nil. Move it above the private call

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

private

def post_params
  params.require(:post).permit(:title, :content)
end
Santhosh
  • 28,097
  • 9
  • 82
  • 87
  • Once ruby encounters `private`, all methods after that will be private, not just the one directly below the `private` command (unless it encounters a `protected` or `public` command later.) Best practice is to list all of your `private` methods together first (this doesn't require `private` -- that's the default), then your `protected` methods together and then all of your `private` methods together. This avoids each from appearing more than once and makes the code a lot less confusing. – Steven Hirlston Dec 14 '13 at 23:44