How to test if parameters exist in rails
You want has_key?
:
if(params.has_key?(:one) && params.has_key?(:two))
Just checking if(params[:one])
will get fooled by a "there but nil" and "there but false" value and you're asking about existence. You might need to differentiate:
- Not there at all.
- There but
nil
. - There but
false
. - There but an empty string.
as well. Hard to say without more details of your precise situation.
Rails check if params exist in the controller
The problem is this line params.require(:primary)
saying that the parameter is required, so attempting to do like params.require(:primary).exists?
wont help you if you do not have a :primary
param at all because the require
already failed.
You need to check its existance on params
itself. For example params.has_key?(:primary)
.
Dependening on your use case, you might also use params.permit(:primary)
directly on params
as well. See the API docs for detailed information on how ActionController::Parameters
(the class for params
) can be used.
http://api.rubyonrails.org/classes/ActionController/Parameters.html#method-i-permit
How to check if params[:some][:field] is nil?
if params[:report] && params[:report][:start_date] && params[:report][:end_date]
How to check if nested params exist?
If you're using Ruby >= 2.3.0, you can make use of Hash#dig
:
params.dig(:q, :category_id)
How to check the existence of a key in params in Rails?
You can not rely on params[:page]
, you need to make sure that the value exists. Moreover, since you are trying to add an integer, you also need to make sure that the type matches.
if params.has_key?(:page)
current_page_count = params[:page].to_i
end
So you can use the method: has_key?
to ensure that the value of :page
exists in params
.
How to check if value exists in params.permit?
You should add validations to your model.
From your question i understand that you want to save details only if you get values in all the field, if not you don't want to save, right?. If yes, then adding validations to your model will give you what you wanted.
Add the following to your organization model
validates_presence_of :name
validates_presence_of :description
validates_presence_of :copyright
by doing so, the user won't be allowed to save the details unless and until all three fields have some value in it.
There is no need to use delete as the incomplete information will not be saved.
for more and advanced info click here
how to check for existence params in view
Views should NOT know about params
.
In controller (or view helper) create variable like `
if params[:publication]
@selected = params[:publication][:category]
end
or even do it in 1 line
@selected = params[:publication].try(:[], :category)
Related Topics
Add Space After Commas Only If It Doesn't Already
Can't Install Gems Because "Undefined Method 'Invoke_With_Build_Args' for Nil:Nilclass"
Ruby: Inject Issue When Turning Array into Hash
Use Rvm to Force Specific Ruby in Xcode Run Script Build Phase
Ruby Custom Error Classes: Inheritance of the Message Attribute
Ruby Modules - Included Do End Block
What Is Recursion and How Does It Work
How to Configure Ruby to Enter the Debugger on Ctrl-C (Sigint)
Remove from the Array Elements That Are Repeated
Downloading Existing Paypal Payments with Rest API
What's the Best Way to Unit Test Protected & Private Methods in Ruby
Replace Words in a String - Ruby
Ruby Class Inheritance: What Is '<<' (Double Less Than)
Reflection on Method Parameters in Ruby