Resolve Fixnum error [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 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?

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? "#"

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

ruby on rails beginner: rails for zombies code sample [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 4 years ago.
Improve this question
where does that #user in the code in check_ammo come from?
see code for "weapon" in model, controller, mailer and schema.rb
rails for zombies code
That code cant be right.
Weaponmailer
def low_ammo(weapon, zombie)
attachments["weapon.jpg"] = weapon.picture_file
mail to: zombie.email, subject: "#{weapon.name} has low ammo"
end
expects 2 parameters in controller
WeaponMailer.low_ammo(#user).deliver
#user is not defined unless its defined in ActiveRecord::Base what is not the case i think. Its just broken code example

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

How to get a ruby class when it's name is in a variable? [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 9 years ago.
Improve this question
So I'm passing a string with the name of an activerecord model class into my method and I want to be able to call some methods on that class.
This is wrong:
def perform(body_class, body_id)
body = body_class.constantize
request = body.find(body_id)
end
Update - that actually works fine.
What error are you getting? Make sure the string is in the correct ActiveRecord format.
For example:
"Person".constantize # works
"person".constantize # does not work
To get the second one to work you should also add camelize
"person".camelize.constantize # works

Resources