Bowo Keren
Bowo Keren

Reputation: 33

Refactor rails params in controller to check if params present

How to refactor this code for more DRY controller?

@places = Place
@places = @places.address(params[:address]) if params[:address].present?
@places = @places.address(params[:name]) if params[:name].present?
@places = @places.price_greater_than_equal params[:price_from] if params[:price_from].present?
#and more..

method address is build from scope in model
sorry for bad english

Upvotes: 0

Views: 211

Answers (1)

Richardlonesteen
Richardlonesteen

Reputation: 594

Why are you not using the Rails Way ? if params are present they will be updated. the validations should be in the model in necessary.

 def update
    @places.update(places_params)
    respond_with(@places)
  end

private
def set_places
  @places = Places.find(params[:id])
end

def places_params
  params.require(:places_params).permit(:address, :postcode, address_attributes: :address (nested attributes)) 
end

Upvotes: 1

Related Questions