Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined method `update' for nil:NilClass post scaffold

I am trying to update a post, and ever since I added the Redcarpet gem, I get an error when I try to update a post.

Here is the error: undefined method 'update' for nil:NilClass

Here is my posts controller:

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]

  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
      @post = Post.find_by_urlid(params[:urlid])
  end

  # GET /posts/new
  def new
    @post = Post.new
  end

  # GET /posts/1/edit
  def edite
  end

  # POST /posts
  # POST /posts.json
  def create
    @post = Post.new(post_params)
      @post.urlid = SecureRandom.urlsafe_base64

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Your Post was successfully created!' }
        format.json { render :show, status: :created, location: @post }
      else
        format.html { render :new }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /posts/1
  # PATCH/PUT /posts/1.json
  def update
    respond_to do |format|
      if @post.update(post_params)
        format.html { redirect_to @post, notice: 'Your Post was successfully updated!' }
        format.json { render :show, status: :ok, location: @post }
      else
        format.html { render :edit }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post.destroy
    respond_to do |format|
      format.html { redirect_to posts_url, notice: 'You have successfully deleted the Post!' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_post
        @post = Post.find_by_urlid(params[:urlid])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def post_params
        params.require(:post).permit(:urlid, :author, :title, :body, :likes, :dislikes, :tags)
    end
end

I've look at other posts, and their answers don't work. Any ideas?

Edit:

Server Log:

    Started PATCH "/posts/sfqm5y99cbomqh4nmuxq5w" for 127.0.0.1 at 2014-10-31 13:05:07 -0700
Processing by PostsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"FRRgB2pI6GAQa6YL8KLZrUShshjjGd7+IXrLv1hTi5E=", "post"=>{"author"=>"Un3qual", "title"=>"First post", "body"=>" on new *dev* system **:D**", "likes"=>"9999", "dislikes"=>"0", "tags"=>""}, "commit"=>"Update Post", "urlid"=>"sfqm5y99cbomqh4nmuxq5w"}
  Post Load (0.1ms)  SELECT  "posts".* FROM "posts"  WHERE "posts"."urlid" = 'sfqm5y99cbomqh4nmuxq5w' LIMIT 1
Completed 500 Internal Server Error in 1ms

NoMethodError (undefined method `update' for nil:NilClass):
  app/controllers/posts_controller.rb:46:in `block in update'
  app/controllers/posts_controller.rb:45:in `update'


  Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.4ms)
  Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (0.6ms)
  Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.6ms)
  Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (8.9ms)


Started PATCH "/posts/sfqm5y99cbomqh4nmuxq5w" for 127.0.0.1 at 2014-10-31 15:42:18 -0700
Processing by PostsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"FRRgB2pI6GAQa6YL8KLZrUShshjjGd7+IXrLv1hTi5E=", "post"=>{"author"=>"Un3qual", "title"=>"First post", "body"=>" on new *dev* system **:D**", "likes"=>"9999", "dislikes"=>"0", "tags"=>""}, "commit"=>"Update Post", "urlid"=>"sfqm5y99cbomqh4nmuxq5w"}
  Post Load (0.3ms)  SELECT  "posts".* FROM "posts"  WHERE "posts"."urlid" = 'sfqm5y99cbomqh4nmuxq5w' LIMIT 1
Completed 500 Internal Server Error in 4ms

NoMethodError (undefined method `update' for nil:NilClass):
  app/controllers/posts_controller.rb:46:in `block in update'
  app/controllers/posts_controller.rb:45:in `update'


  Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.4ms)
  Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (0.6ms)
  Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.6ms)
  Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (10.2ms)
like image 901
Un3qual Avatar asked Aug 17 '26 11:08

Un3qual


1 Answers

"Post.find_by_urlid(params[:urlid])" code has deprecated "find_by_" method. Rails 4 has deprecated "find_by_" methods. [Link]: http://edgeguides.rubyonrails.org/4_0_release_notes.html

Try Using: Post.find_by(urlid: params[:urlid]). There is a typo error in "edite" method.

like image 184
Pavan Avatar answered Aug 19 '26 11:08

Pavan