What does this controller code do? [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 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

Related

How to find out the number of records in a table? [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
I have a scaffold help requisition. There is a status field. Each request can have 3 statuses (pending, in_progress, finished), implemented through enum. It is necessary to write methods that will count the number of requests with each status. For example, method A counts how many total helper prequests with status 0 (pending). I understand that there is nothing complicated about this, but I can't understand and could use some help.
It would be something like this on your model:
def self.pending_count
where(status: 0).count
end
def self.in_progress_count
where(status: 1).count
end
def self.finished_count
where(status: 2).count
end
Then you can just call Model.pending_count (or the other methods) wherever you need it.

Active Record doing nothing [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
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....

Making mailer functions more DRY [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 5 years ago.
Improve this question
Inside a mailer file, I have 11 methods all of which start with the line
#reservation = reservation
Is there a way to make this DRY? I tried the following:
def set_reservation
#reservation = reservation
end
and then
before_action :set_reservation
Unfortunately, this always gave me something along these lines:
AgentReservationMailer#send_reserve_complete_mail: processed outbound mail in 1.7ms
NameError: undefined local variable or method `reservation' for #<AgentReservationMailer:0x007ffc9ae5bb38>
I'm still a very junior level developer, but I would like to try and make things look as professional as I can - is what I am trying to do even possible though?
The reason you're seeing the error is that the mailer does not know of a variable reservation inside the set_reservation method. I'm assuming that the 11 methods you mention, which use the
#reservation = reservation
take reservation as an argument. As it stands, there really is no need to try and reduce duplication.
As a side note, DRY is not a principle you should follow blindly. If you had a couple of lines that were the same in each method, then that would indeed justify an "extract method" refactoring. But replacing that #reservation = reservation assignment with e.g. a method call set_reservation(reservation), you'd still end up repeating one line across all methods.

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 %>

undefined method `days_in_a_month' for Time:Class [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
I tried several things and I can't seem to find the reason why Time::days_in_a_month is not found.
I'm trying to calculate the amount of signups for each day of the year, this is the code:
require 'date'
require 'time'
class GraphicsController < ApplicationController
def year_graph(year=2013)
data=calculate_signups(year)
gon.data=data
end
def calculate_signups(year)
#binding.pry
month_days=(1..12).map{|x| Time.days_in_a_month(x,year)}
total_yeardays=month_days.sum
year_sum=Array.new
day_sum={day:0,yearday:0,num:0}
afee=User.clients.year(year)#.month(month)
month_days.each_with_index do |mindex,mdays|
(1..mdays).each do |d|
day_fees=afee..month(mindex).day(d)
dia=day_sum.dup
suma=day_fees.length
dia[:day]=d
dia[:num]=suma
dia[:yearday]=Date.new(year,mindex,d).yday
year_sum << dia
end
end
year_sum
end
end
If I try it in the debugger pry(see binding.pry line) there is no problem. And of course no problem either when I try it on the console rails c --sandbox. I assume I'm missing something, but I don't know what!
The correct syntax is without a. The correct is
Time.days_in_month

Resources