In Rails, How can I convert params[:id] to Integer [closed] - ruby-on-rails

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

Related

How to check if parameters/string contains a substring? [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 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? "#"

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?

Use params value with variable in ruby on rails? [duplicate]

This question already has an answer here:
Use variable in parameter ruby on rails? [closed]
(1 answer)
Closed 7 years ago.
I want to check if param key exists with a variable name and if it exists I want to use value something like params[filenamestring[-1]].
filenamestring is any array generate with split
If you variable is a hash than here is your answer
if params.has_key?(filenamestring[-1])
param_value = param[filenamestring[-1]]
end
You can check existence of key using has_key? method:
key = filenamestring[-1]
# or, key = filenamestring.last
if params.has_key?(key)
value = params[key]
# do stuff with value
end

Use variable in parameter ruby on rails? [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
I want to check if param key exists with a variable name and if it exists I want to use value something like params[filenamestring[-1]].
filenamestring is any array generate with split
generally we use params like params[:key] but here i have array and want to use params value with array last element like params[filenamestring[-1]]
You are looking for this:
if params.key?(filenamestring[-1])
This will check if the key exists within the params.
Edit: Something like this would add the param to an array:
my_array << params[filenamestring[-1]] if params.key?(filenamestring[-1])
Or to add it to a string or integer:
my_variable + params[filenamestring[-1]] if params.key?(filenamestring[-1])
If you are doing something else, let me know and I'll update my answer again.

Resources