Active Record doing nothing [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 3 years ago.
Improve this question
please I need a little help... I don't know what I'm doing wrong but I need just a simple select query with Active Record. This looks my code:
Model:
class Kiosk < ApplicationRecord
#kiosk = Kiosk.all
end
Controller:
class KioskController < ApplicationController
def kiosk
#kiosk = Kiosk.all
end
end
HAML:
##kiosk
And it just doing nothing. Even if I change a password of database there isn't any error with connection. rake db:migrade was done a db was created.
Thanks

You may want to look at your logs (eg Rails.root => logs/development.log) or the output in your terminal – are there any error messages? If you log in to your console with eg rails console and run #kiosks = Kiosk.all – what are you seeing? or how about Kiosk.count –– is it showing that there are any kiosks? As mentioned the model looks funny... not sure why you have the #kiosk = Kiosk.all line in there at all....

Related

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

Handling Ruby Standard Error? [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
I'm using the FedEx gem to pull down tracking numbers, the issue I'm running into is that if a tracking number has been generated, but not picked up, the gem returns a Fedex::RateError
According to the documentation Fedex:RateError inherits from StandardError I need to display a message if this error trips on the frontend site
I looked around online and found some ideas, but was hoping someone can point me in the right direction for handling errors, I'm using Rails 4.
Thanks for any suggestions
begin
# Do your normal happy path view stuff here
rescue Fedex:RateError => error
# Do your display of the error to the user here
end
Thanks msergeant, that did it!
Here's my final code- this recovery isn't very well documented and seems like a handful of people we're looking for it.
begin
results = fedex.track(:tracking_number => tracking)
rescue Fedex::RateError
statusImg = "http://t2.gstatic.com/images?q=tbn:ANd9GcRuKAIYZ2mNLsjRulsH05zNwF93jmAdpgZGSgtVN8XiT7_SWw285g"
statusRet = "Nope!!"
#trkResultTxt = statusRet
#trkResultImg = statusImg
else
tracking_info = results.first
#trkStatus = tracking_info.status
end

How do I access data in the view from my controller? Rails [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 8 years ago.
Improve this question
I have data I'm getting in my controller and want to display it in the view. I've tried using a helper method but no luck. I've also tried an instance variable but still doesn't work.
You can do so like this :
class SomeClass < ApplicationController
def index
#something = 'This is a cool text'
end
end
In the index.html.erb you can do this :
<%= #something %>

What does this controller code do? [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
I've come across this pattern a few times, but am not sure what it is, or how to Google it.
class ApiController < ApplicationController
class InvalidAppToken < RuntimeError ; end
class InvalidUserToken < RuntimeError ; end
...
end
It creates custom errors. Doing this you can raise your personalized errors, then rescue them to better handle the app errors.
class MyController < ApiController
begin
unless DoSomething(params[:user_token])
raise InvalidUserToken
[...]
end
rescue InvalidUserToken
# manage this error
end
end
Here is a good option for searching the web with special symbols considered (most major search engines ignore special characters in searches):
http://symbolhound.com/?q=ruby+%3C+runtimeerror
It simply defines custom types of errors. Could be useful to raise very specific errors in your workflow.
Have a look at this book

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