我正在尝试更新帖子,自从我添加了Redcarpet gem之后,当我尝试更新帖子时出现错误。
以下是错误:undefined method 'update' for nil:NilClass
这是我的帖子控制器:
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)
答案 0 :(得分:2)
&#34; Post.find_by_urlid(PARAMS [:URLID])&#34;代码已弃用&#34; find_by _&#34;方法。 Rails 4已弃用&#34; find_by _&#34;方法。 [链接]:http://edgeguides.rubyonrails.org/4_0_release_notes.html
尝试使用:Post.find_by(urlid:params [:urlid])。 &#34; edite&#34;中存在拼写错误。方法
答案 1 :(得分:1)
基于您对问题的评论,这种情况正在发生,因为此urlid没有记录。
您应该像这样更改回调(before_action)过滤器。
def set_post
@post = Post.find_by(urlid: params[:urlid])
redirect_to posts_path, flash: {error: "The post does not exists"} if @post.nil?
end
这将重定向到帖子的索引,并显示错误消息(您应该在布局中显示错误的div或模板中的错误,以便显示)。
如果@post
存在,那么它将正常转发到该动作。
答案 2 :(得分:0)
如果您没有使用任何实例化@post
变量的预加载器,那就是问题所在。对于update
行动,你应该:
def update
@post = Post.find params[:id]
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