How to check if parameters/string contains a substring? [closed] - ruby-on-rails

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
For example, if I submit a text. I want to check if it contains some string.
I am trying to check if submited text contains "#" or not. params[:field].include? "#" doesn't work in rails. It says undefined "include" method.

If params[:field] is nil, then you can't invoke include? on it, as it isn't a method defined in the NilClass class.
If what you want is to to check if submitted text contains "#", you can use =~:
p { field: 'foo#bar' }[:field] =~ /#/ # 3
p nil =~ /#/ # nil

In this particular case params[:field] is nil and the #include? method isn't available for nil objects. That's why you're getting an error.
Converting it to a string will make the object available for an #include? method call and remove the possibility of the object being nil:
params[:field].to_s.include? "#"

Related

How to use or in checking presence? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
i need to check presence of a value in an array. It can be two values i need to check with an or case if it includes in the array. I think it will be confusing from my description you will understand after you see the code below.
if user.sheet_ids.include? params[:id].to_i || params[:sheet_id].to_i
can :show, Project
end
It returns false even if sheet_id is present in the array of sheet_ids.
user.sheet_ids is an array and i need to check if any of the value includes in this array. The method i used is not seems to be working.
Is there any simple way of doing this. Thanks.
I would do something like this:
(user.sheet_ids & params.values_at(:id, :sheet_id).map(&:to_i)).any?
user.sheet_ids.include? params[:id].to_i || params[:sheet_id] is executed in this way user.sheet_ids.include?(params[:id].to_i || params[:sheet_id]). The OR statement is executed and the result is what is passed as an argument to user.sheet_ids.include?
To check if either params[:id].to_i or params[:sheet_id] is included in the array users.sheet_ids, you need to do this;
if user.sheet_ids.include?(params[:id].to_i) || user.sheet_ids.include?(params[:sheet_id].to_i)
can :show, Project
end

Deep Rails JSON parse [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Is it possible to have JSON.parse work 2 layers deep so that a hash within a hash will get parsed as well? Is there a method for or do I have to do something like JSON.parse(JSON.parse(...)['foo'])?
JSON.parse doesn't care about your hash structure:
> str = JSON.dump({foo: {bar: {baz: :qux}}})
=> "{\"foo\":{\"bar\":{\"baz\":\"qux\"}}}"
> p = JSON.parse(str).with_indifferent_access
=> {"foo"=>{"bar"=>{"baz"=>"qux"}}}
> p[:foo][:bar][:baz]
=> "qux"
(Well, it cares if you have a malformed string, but that's something else altogether.)
So, what are you asking?

Resolve Fixnum error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a parameter (params[:authorization]) that comes from the URL, as you can see:
authorization_selected = params[:authorization]
new_parcel = params[:new_parcel].to_i
puts authorization_selected.class (in the console show type String)
puts new_parcel.class (in the console show type Fixnum)
In my controller, have:
#portability = Portability.new
#portability.employee_id = authorization_selected.employee_id
However this returns an error:
undefined method `employee_id' for 3:Fixnum
I need that both was integer. How do it?
You are calling the employee_id method on authorization_selectedwhich is a String and does not provide this method.
Obviously this does not work. You probably want to do
#portability = Portability.new
#portability.employee_id = authorization_selected
assuming that params[:employee] contains the employee_id and Portability is an ActiveModel or an ActiveRecord.
Perhaps you can change your form that the value can be assigned through the initializer?

In Rails, How can I convert params[:id] to Integer [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Guys I am trying to retrieve specific records from the DB using params[:id].
x = Authors.find(params[:id])
We cannot use FIND with string like this because the parameter is always passed by HTTP as a string:
I also tried this code but I do not know why it always returns 0 while the passed parameter is 4
x = Authors.find(params[:id]).to_i
So how can I do something like casting for ID Parameter or Any method to convert HTTP passed strings to integers
You don't really have to convert strings to integer to use find. It should be able to convert the string for you.
Just to give an example:
Author.find(2) # => #<Author:1234>
Author.find("2") # => #<Author:1234>
Author.find("2") == Author.find(2) # => true
Try using pry to see if what you are passing is correct. Hope this helps clear out the method.
I think you might have just misplaced your type casting:
x = Authors.find(params[:id].to_i)
you can use this:
x = Authors.find(params[:id].to_i)
However,
x = Authors.find(params[:id])
is also correct as it returns the correct author.
Before using the above one you can check whether the params[:id] is present or not:
if params[:id].present?
x = Authors.find(params[:id])
end
After I used the method .inpect on the parameter which I am trying to grab, I found that it was nil. I was grabbing it wrongly. By the way, I was trying to grab another parameter not :id but :artCatName and Here are my passed parameters:
{"utf8"=>"✓", "authenticity_token"=>"tQetEjfE+stm/yXfQWyS9pEhznu+zeNwrHPa6BNjWsfd4bcv/PFsYRcMT‌​1L8y2obJt6xNTcoOFNgzq6R6OcjMw==", "category"=>{"artCatName"=>"1"}, "article"=>{"artTitle"=>"bb", "artBody"=>"b"}, "commit"=>"Publish", "controller"=>"articles", "action"=>"create"}
Here how I grabbed :artCatName and how I converted to integer
params[:category][:artCatName].to_i

Ruby on Rails: Is there a way to make blank form inputs submit nil? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Make blank params[] nil
Is there a way to make blank form inputs submit nil? Right now, I'm going through and in a before_save manually converting all "" into nil.
This really doesn't seem very DRY, and I feel like I must be missing something.
Check out the attribute_normalizer plugin.
There is no notion of "submitting nil" from a browser.
If field of name 'foo' is not submitted, then if you ask for params[:foo], it will return nil, which seems to be the behavior you desire.
Can you explain more what you want to do?

Resources