Reputation: 571
I'm trying to execute the update action but I keep getting the error "This specialize already exist" even though I put the name completely different. It appears that there's something wrong with my nested if in the Update action, but I used the same condition in the Create action but it works fine. I'm new to rails so forgive me if this is an obvious and stupid question. Here is my action controller code:
class SpecializesController < ApplicationController
before_action :set_specialize, only: [:show, :edit, :update, :destroy]
# GET /specializes
def index
@specializes = Specialize.all
end
# GET /specializes/1
def show
end
# GET /specializes/new
def new
@specialize = Specialize.new
end
# GET /specializes/1/edit
def edit
end
# POST /specializes
def create
if(specialize_params[:name].present? && specialize_params[:specialize_category_id].present?)
@specialize = Specialize.new(specialize_params)
if Specialize.where(:name => @specialize.name).blank?
@specialize.save
redirect_to specializes_url, notice: 'Specialize was successfully created.'
else
redirect_to specializes_url, notice: 'This specialize already exists.'
end
else
redirect_to specializes_url, notice: 'One or more input is blank. Add failed!'
end
end
# PATCH/PUT /specializes/1
def update
if(specialize_params[:name].present? && specialize_params[:specialize_category_id].present?)
if Specialize.where(:name => @specialize.name).blank?
@specialize.update(specialize_params)
redirect_to specializes_url, notice: 'Specialize was successfully updated.'
else
redirect_to specializes_url, notice: 'This specialize already exists.'
end
else
redirect_to specializes_url, notice: 'One or more input is blank. Update failed!'
end
end
# DELETE /specializes/1
def destroy
@specialize.destroy
redirect_to specializes_url, notice: 'Specialize was successfully destroyed.'
end
private
# Use callbacks to share common setup or constraints between actions.
def set_specialize
@specialize = Specialize.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def specialize_params
params.require(:specialize).permit(:name, :specialize_category_id)
end
end
Upvotes: 1
Views: 88
Reputation: 6263
Inside your update method, the result of @specialize.name is nil
from statement if Specialize.where(:name => @specialize.name).blank?
is the reason your code always go to else block
I suggest if you do not want to use double for @specialize.name, you can use validation rule open your model specialize.rb and add below is check so name must be unique
validates :name, :presence => { message: 'must be present'}, uniqueness: { case_sensitive: false , message: 'duplicate name, please enter new name'}
Upvotes: 1
Reputation: 9491
You can do the following things to debug this:
@specialize.update(specialize_params)
will return a boolean value which you can use to check if the validations are passing or not.
You can also do @specilize.errors
to see if there were any errors when trying to save the record.
Upvotes: 1