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 5 years ago.
Improve this question
My rails app is receiving the following from the json. How do I access the name key of attributes and the calendar id in Ruby.
Parameters: {"data"=>{"type"=>"user-events", "attributes"=>{"name"=>"An event", "location"=>"University of Illinois at Urbna-Champaign", "notes"=>"Testing my eventf\n", "all-day"=>false, "recurring"=>false, "sunday"=>false, "monday"=>false, "tuesday"=>false, "wednesday"=>false, "thursday"=>false, "friday"=>false, "saturday"=>false, "start-date"=>"01-16-2018", "stop-date"=>"01-16-2018", "start-time"=>"04:32PM", "stop-time"=>"05:32PM"}, "relationships"=>{"calendar"=>{"data"=>{"type"=>"calendars", "id"=>"685"}}}}}
Do I use the code below to access the name attribute ?
params[:data][:attributes][:name]
Also, do I use the following to access the id for the calendars ?
params[:data][:relationships][:calendar][:data][:id]
Thanks in advance for your help
If your Hash name is Parameters
Parameters['data']['attributes']['name'] # "An event"
Parameters['data']['relationships']['calendar']['data']['id'] # "685"
if it's params, then
params['data']['attributes']['name'] # "An event"
params['data']['relationships']['calendar']['data']['id'] # "685"
will access the attributes.
Related
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\"}"
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.
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
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
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
What is the proper way to update a record in Ruby on Rails? Just a single record. For example, I want to modify the name of the title somehow. A code snippet would be awesome!
The basic way to update a record is like that :
#user.name = "new name"
#user.save
this assumes that #user in an instance of a User class having a name field.
you can also do mass_assignment with the update_attributes method
#user.update_attributes name: "new name", email: "new_email#foo.com"
If you want an exception to be raised if the record is invalid you can use the bang method instead, so here it would be save! & update_attributes!
You can also use the update_attribute which has been (or will be soon) renamed to update_column to update a single column while skipping the validation, but you should generally avoid using this method
more doc there
Finally you can use the write_attribute method
entity = Entity.find_by_name("some_name")
entity.title="new title"
entity.save!
or
Entity.find_by_name("some_name").update_attributes(:title => "new title")
for more details, refer to http://guides.rubyonrails.org