Use variable in parameter ruby on rails? [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
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.

Related

Random name ( RUBY ) PLEASE [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 1 year ago.
Improve this question
Have the function Cus_free_makers_eg1heChallenge(str) take the name parameter being passed to return "Hello Cristina!" if the name parameter is equal to "Cristina", return "Hello Kay!" if the name parameter is equal to "Kay" or "Hello there!" if the name parameter is equal to any other.
def Cus_free_makers_eg1heChallenge(str)
return str
end # keep this function call here
puts Cus_free_makers_eg1heChallenge(STDIN.gets)
Welcome to Stack Overflow! This is a bit off-topic, but first off, you should probably keep the method name shorter and simpler - probably not more than one or two words. Otherwise, when you call the method, it's a mouthful to type. The name should also describe what the method does, so the code is more readable.
Here is how I would write the method:
def hello(name)
if name == "Cristina" or name == "Kay"
return "Hello #{name}!"
end
"Hello there!"
end

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

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.

How to turn a string into URL in 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 8 years ago.
Improve this question
If I have a form input where someone puts 'facebook.com' or 'google.com' it turns it into a valid URL adding http:// so that rails can use it. What I want is to have a form where you input a URL and it grabs the 3 most used words at that site and shows them in a list on the results page. This list should be accessible later, so I also need to store those words with that URL
def smart_add_url_protocol
url = Url.find_by(params[:url])
unless self.url[/\Ahttp:\/\//] || self.url[/\Ahttps:\/\//]
self.url = "http://#{self.url}"
end
end
something like this.
You can use URI::HTTP#build like so:
URI::HTTP.build(host: 'facebook.com').to_s
#=> "http://facebook.com"
I recommend the Addressable gem which is somewhat smart about guessing URLs from strings:
require "addressable/uri"
Addressable::URI.heuristic_parse(some_string_with_an_url).to_s

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