Reflect validation in the ʻupdate action of
controller.rb`.
It is written from @ post.valid?
in the if statement of error handling of the update action.
With this writing method, validation will be effective for information that has already been registered.
post_controller.rb
def update
if @post.valid?
@post.update(post_params)
flash[:notice] = "Editing is complete"
redirect_to post_path(@post.id)
else
flash.now[:alert] = "Editing failed"
render :edit
end
end
By starting writing from @ post.update (post_params)
, validation will work normally even with updates.
post_controller.rb
def update
if @post.update(post_params)
flash[:notice] = "Editing is complete"
redirect_to post_path(@post.id)
else
flash.now[:alert] = "Editing failed"
render :edit
end
end
Recommended Posts