123
123

Reputation: 8951

RoR - Keep track of invalid ID 's in index method

I want my index() method to let me know when an invalid ID has been passed in, but I don't want the page to barf with a 500. I want (and have set up) the index method to skip model ID's it's unaware of - However, I want to keep track of those invalid model ID's somehow.

authorize :read
def index
  modelIds = []
  validIdList = ""
  models = ModelType.get_all_model_names()
  models.each do |model|
    id = ModelType.get_id(model)
    modelIds.push(id)
  end
  validIdList = modelIds.join(', ')
  @filter = "model_type_id IN (#{validIdList})"
end

How can I keep track of the invalid ID's that index() runs into?

Upvotes: 0

Views: 75

Answers (1)

Austio
Austio

Reputation: 6095

if you have standard resources resources :something then if you mean an invalid route like this

/something/#{invalid_id}

Then you will not catch these in the index action, they will be in the show action. You could accomplish this in a before_filter in order to slightly reduce the amount of conditional logic in your show action.

In controller

before_filter :check_valid_id, only: [:show]

and in your private section of controller

def check_valid_id unless find_something log(params[:id]) redirect_to :index end end

You may also want to check_valid_id in other member routes such as edit, update, destroy and provide a flash message that the id was invalid

Upvotes: 1

Related Questions