I am using this function to find users, which i am using .require
only worked when i sent both or at least one parameter but if i send empty i got errors, It should not be mandatory to send parameters
def find_params
params.require(:person).permit(:name, :age)
end
if i send the name or age i will work, but if i send nothing i am getting this error
params is missing
if i send empty like this:
{}
or null
it should work correctly returning all the users
or should not i use this to search users?
params.require(:person).permit(:name, :age)
might i have to user like this?
params[:name] and params[:age]
i am working with reactjs i am sending the payload liket this:
{name:"ed", age:"12", skin:"black", weight: "180lbs", height:"183"}
CodePudding user response:
Rails' StrongParameters
were built for a very specific use:
It provides an interface for protecting attributes from end-user assignment. This makes Action Controller parameters forbidden to be used in Active Model mass assignment until they have been explicitly enumerated.
That means when you use the params
just to read from the database, then there is no need (and hardly any advantage) to use StrongParameters
.
Instead, I would just use the params
directly in the controller like this:
def index
@users = User
.filter_by(:name, params.dig(:person, :name))
.filter_by(:age, params.dig(:person, :age))
# ...
end
And to make this work you will need to define an filter_by
scope in your app/models/user.rb
:
scope :filter_by, -> (attr, value) { where(attr => value) if value.present? }
CodePudding user response:
The whole point of using ActionController::Parameters#require
is to cause your create/update method to bail early if the parameter you expect to be a hash isn't sent at all since there is no point in proessing the request further and this prevents a potential uncaught nil error.
If you want to allow a key to be null use #fetch
instead:
params.fetch(:person, {})
.permit(:name, :age)
#fetch
allows you to pass a second key which is the default value and it returns a new ActionController::Parameters instance.
But it looks like you're actually sending flat parameters which are not nested in which case you don't need fetch either:
params.permit(:name, :age)
Note that Rails by default has parameters wrapping turned on for JSON requests and both will very likely work.
CodePudding user response:
You can use Find User
Routes.rb
get "search_user", to: "users#search_user"
controllers/users_controller.rb
def search_user
@users = User.search(params[:name], params[:age], params[:skin], params[:weight], params[:height]) // search name or age
// you can use byebug to check @users
end
models/user.rb
def self.search(name, age, skin, weight, height)
if name.blank? & age.blank? & skin.blank? & weight.blank? & hight.blank?
all
else
where('name LIKE ? OR age LIKE ? OR skin LIKE ? OR weight LIKE ? OR height LIKE ?', "%#{name}%", "%#{age}%", "%#{skin}%", "%#{weight}%", "%#{height}%")
end
end
=> This is my way which i used. Hope to help you.
CodePudding user response:
If you want to allow the find_params
function to handle empty or missing parameters, you can modify it to use the permit method on the params object directly, rather than using the require method. This will allow you to handle cases where the :person parameter is missing or empty.
Here is an example of how you could modify the find_params
function to handle empty or missing parameters:
def find_params
params.permit(:name, :age)
end
With this modification, the function will still return the :name and :age parameters if they are present, but it will not raise an error if they are missing. If you want to allow all parameters, you can use the :person
parameter instead of specifying the individual parameters:
def find_params
params.require(:person).permit!
end
This will allow the function to accept any parameters that are passed in the :person
parameter.