开发者

Where to check and validate non model parameters in Rails

Where do you check URL parameters that are not model attributes (like page, per_page, sort_mode) in Ruby On Rails? In the controller or in the model?

For example, when doing a more complicated database query, would you check the params and maybe set defaults in the controller and then do for example MyModel.search(page, per_page, order, sort_mode, query), or would you setu开发者_如何学JAVAp the validation inside the model and just pass the non manipulated params MyModel.search(params)?

And how do you report that parameter back to the view? For example a sort_mode parameter that should result in a little arrow on the view for sort direction. Do you check and clean up the params hash and get the data in the view from params, or do you use an own instance variable for that?


I tend to sanitise params in the controller.

class ApplicationController < ActionController::Base
  before_filter :sanitise_params

  protected

  def sanitise_params
    # tidy up
    # set defaults
  end
end

Good practice that the Models declare their interface and it's up to the Controllers to talk to them in the correct way. That way you have a clear separation of your layers.

View helpers are there to help with the views. Here are some examples that come as part of ActionPack's ActionView. You can put your own in app/helpers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜