ruby-on-rails - 未定义方法 `update' 为 nil :NilClass post scaffold

标签 ruby-on-rails ruby

我正在尝试更新帖子,但自从我添加了 Redcarpet gem 后,我在尝试更新帖子时遇到错误。

这是错误:undefined method 'update' for nil:NilClass

这是我的帖子 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

我看过其他帖子,但他们的答案不起作用。有什么想法吗?

编辑:

服务器日志:

    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)

最佳答案

“Post.find_by_urlid(params[:urlid])”代码已弃用“find_by_”方法。 Rails 4 弃用了“find_by_”方法。 【链接】:http://edgeguides.rubyonrails.org/4_0_release_notes.html

尝试使用:Post.find_by(urlid: params[:urlid])。 “编辑”方法中存在拼写错误。

关于ruby-on-rails - 未定义方法 `update' 为 nil :NilClass post scaffold,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26682953/

相关文章:

Ruby 类方法。它是否在主要对象上被调用?

mysql - 不调用rails sql数据库中的表参数

ruby-on-rails - 类似于 Rails 的桌面应用程序框架

sql - Rails 根据一列和另一列(如果为空)对事件记录结果进行排序

ruby-on-rails - rails 功能测试

jQuery-UI 可排序连接列表保存顺序和与 Rails 模型的关联

mysql - 无法安装以前版本的 Sphinx

ruby-on-rails - 连接两个事件记录结果(通过连接表)以返回一个新结果

ruby - 快速解决字谜

ruby - 使用 Ruby 向 Azure 进行身份验证(HTTP header 身份验证)?