Rails Validating Search Params

Validate params in Rails

I would probably move this logic to the model, but if you really want it in the controller you could simplify it.

def validate_filer
return true unless params.has_key?(:filter)
['popular', 'following', 'picks', 'promoted', 'first-posts'].include?(params[:filter])
end

Rails validating search params

Actually what you are doing is (almost) best practice and will (almost) be incorporated in Rails 4 with strong parametsers. (I say almost because your check_max_time looks like it should be a validation in your model.)

You should go ahead and pull in the feature today and make upgrades easier on yourself. Strong Parameters https://github.com/rails/strong_parameters

Documentation is there, but here is how you incorporate it.

class SearchController < ApplicationController
include ActiveModel::ForbiddenAttributesProtection

def create
# Doesn't have to be an ActiveRecord model
@results = Search.create(search_params)
respond_with @results
end

private

def search_params
# This will ensure that you have :start_time and :end_time, but will allow :foo and :bar
params.require(:start_time, :end_time).permit(:foo, :bar #, whatever else)
end
end

class Search < ActiveRecord::Base
validates :time_less_than_six_hours

private

def time_less_than_six_hours
errors.add(:end_time, "should be less than 6 hours from start") if (end_time - start_time) > 6.hours
end
end

Validating search form on rails

So user gets to the index page, and fill the search fields: search and miles
Then click Search and arrive the index controller, where a new search is created: @search = Search.new.

In no place at your code The Search is 'given' the params the user filled.

Shouldn't you do something like :

@search = Search.new(search: params[:search], miles: params[:miles])

or

@search = Search.new
@search.search = params[:search]
@search.miles = params[:miles]

and then for the validations to start, you need to 'activate' them somehow, like @search.valid? which should trigger the validation process

Where validate input params? In the controller or in the service class, used by controller?

I think, that you should do it in controller. As for me, i usually do it in before_action method. According to MVC pattern, all params and routing logic must be in controller.

How to validate Rails model based on a parameter?

I have tried many ways to complete this task,

  1. I used Inheritance - Created a sub class from the User class
  2. Call a method in the model from the controller to set the attribute and bind that attribute with the validation
  3. Use the context option

I guess the context option is the most reliable solution for this issue i faced. So here when i set the context as :interface the model validation will trigger only based on that value

Model - User.rb

class User < ActiveRecord::Base
validates_presence_of :phone_number, on: :interface
end

Controller - users_controller.rb

@user = User.new(user_params)
@save_result = false
if params[:invitation_token] == nil
save_result = @user.save(context: :interface)
else
save_result = @user.save
end

If you use multiple options in ON:

  validates  :terms_and_conditions, acceptance: {accept: true}, on: [:create, :interface], unless: :child
validates :privacy_policy, acceptance: {accept: true}, on: [:create, :interface], unless: :child

Correct way to validate the query string parameter in controller?

You can do this:

def index
filter = %w(pending active deleted).include?(params[:status]) ? params[:status] : ''
@users = User.select_all(filter)
end

It makes the check easier to understand + no need for an instance variable right?

BTW, I feel like:

['pending', 'active', 'deleted']

Should be given by a method, to avoid a magic array



Related Topics



Leave a reply



Submit