undefined method ` #' in rails [closed] - ruby-on-rails

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Does someone know why
hash['City'] = {}
hash['City']['answer0'] = 'foo'
Get the following error:
undefined method `+#' for {"answer0"=>"foo"}:Hash
Thanks

If you want to have 'multi-dimentional' hashes you need to properly define the hash like so:
a = Hash.new { |hash, key| hash[key] = Hash.new(&hash.default_proc) }
Then you can do:
a['city']['answer0'] = 'foo'

Related

If variable is defined set variable, if not defined set null [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 3 years ago.
Improve this question
I want to set a variable if the variable is defined but if it's not defined, set it to nil.
def change(cursor:)
end
I used:
change(cursor: cursor || nil)
But I am getting this error:
NameError: undefined local variable or method `cursor'
Just enter nil as the default value.
def change(cursor: nil)
"cursor = #{cursor.nil? ? 'nil' : cursor }"
end
change(cursor: 'cat') #=> "cursor = cat"
change #=> "cursor = nil"

RoR JSON::ParserError 795: unexpected token [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 need save {"individual"=>"true", "multi-user"=>"true"} to a string and then parse it to a JSON representation of the same hash. Unfortunately, this produces the following error:
JSON::ParserError in WizardsController#step
795: unexpected token at '{"individual"=>"true", "multi-user"=>"true"}'
Use to_json on the hash, before you attempt to manipulate it:
{"individual"=>"true", "multi-user"=>"true"}.to_json
=> "{\"individual\":\"true\",\"multi-user\":\"true\"}"

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.

using an external .rb in a controller [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I currently have a separate .rb file that contains smth like this:
if lang == 'fr'
#a = 'AAAAAAA'
#b = 'BBBBBBB'
#c = 'CCCCCCC'
else
#a = 'sadadddsad'
#b = 'dsafdsfdasfdsa'
#c = 'dsadasfdsfsfd'
end
, only with a lot more strings.
Being a large library of strings, and wanting to keep this in one place as these will be used in multiple controllers and functions within, what is a good method to call these files from inside a function in controllers?
Looks like you want to use Internationalization in your app. You could follow this link http://guides.rubyonrails.org/i18n.html to know more and its implementation.

Ruby attribute meta programming [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 8 years ago.
Improve this question
My user has the attribute:
:step1_local
:step2_local
:step3_local
...
...
:local1
:local2
:local3
I would like to change an attribute value based on another set of attributes on the same model. I would like to do some processing mapping on user, say:
def magic (user)
user.local(1..3) = process(user.step(1..3)_local)
end
The code above of course does not work (example). I am not sure how to do it dynamically without going through each attributes individually. I want to map processing one to another. Any ideas?
You can use Object#public_send and method, like this:
def magic(user)
(1..3).each do |n|
user.public_send("local#{n}=", process(user.read_attribute("step#{n}_local")))
end
end

Resources