I have a "Log" project, each log has a :date, :hours, :description. I am simply trying to determine how many hours I have worked in a week, but am having trouble determining the proper separation of code. Let me know if any further code is needed. Rails 3.
log.rb
def self.days_in_range(from, to)
Log.where(:date => (from.to_date)..(to.to_date))
end
index.html.erb
<% content_for :sidebar do %>
<h4> Sidebar Content </h4>
<ul>
<li>Hours worked this week:
<%= Log.hours_this_week %> # unsure how to call
</li>
<li>Hours worked in total:
<%= Log.sum(:hours) %>
</li>
<li>Most hours worked in a day:
<%= Log.maximum(:hours) %>
</li>
</ul>
<% end %>
logs_helper.rb?
def hours_this_week
today = Time.now
day_of_week = today.wday
sunday = today - day_of_week.days
days = Log.days_in_range(today, sunday)
hours = 0
days.each do |day|
hours += day.hours
end
end
[solved] error
Showing /Users/***/Documents/workspace/***/hours_tracker/hours/app/views/logs/index.html.erb where line #33 raised:
undefined method `hours_this_week' for #<LogsController:0x103b66be8>
Extracted source (around line #33):
30: <h4> Sidebar Content </h4>
31: <ul>
32: <li>Hours worked this week:
33: <%= hours_this_week %>
34: </li>
35: <li>Hours worked in total:
36: <%= Log.sum(:hours) %>
Rails.root: /Users/***/Documents/workspace/***/hours_tracker/hours
full trace
[updated] new error
error
ArgumentError in Logs#index
Showing /Users/***/Documents/workspace/***/hours_tracker/hours/app/views/logs/index.html.erb where line #33 raised:
wrong number of arguments (0 for 1)
Extracted source (around line #33):
30: <h4> Sidebar Content </h4>
31: <ul>
32: <li>Hours worked this week:
33: <%= hours_this_week %>
34: </li>
35: <li>Hours worked in total:
36: <%= Log.sum(:hours) %>
Rails.root: /Users/***/Documents/workspace/***/hours_tracker/hours
This is tangential to your question, but I noticed this looking at your hours_this_week method. I could be wrong, but one little thing you may want to look at here is that your hours_this_week method is going to return the collection iterated through in your each statement (i.e. days), not the product of that statement (the new value of hours).
You could either just add the line:
hours
to the end of this method, or use inject instead of each:
# The initial "hours" declaration is no longer necessary,
# because inject returns its result, rather than the
# collection it is iterating through.
days.inject(0) {|hours, day| hours += day.hours }
That line would eliminate the need for your each statement and for explicitly returning hours at the end of your hours_this_week method.
That said, putting the method in logs_helper.rb and calling it with:
<%= hours_this_week %>
would be the way to go.
Assuming your index.html.erb file is in your app/views/logs folder, you can just call hours_this_week directly:
<%= hours_this_week %>
Related
I'm trying to paginate with will_paginate in my rails app.
in my controller I have
def index
params[:per_page] ||= 25
params[:page] ||= 1
#links = Link.order('created_at DESC').page(params[:page]).per_page(params[:per_page])
end
in my view I have
<ul>
<% #links.each do |entry| %>
<li><%= link_to entry.title, entry.url %></li>
<% end %>
<ul>
<%= will_paginate #links %>
I'm getting the error
comparison of Fixnum with String failed
Extracted source (around line #8):
5: </h2>
6:
7: <ul>
8: <% #links.each do |entry| %>
9: <li><%= link_to entry.title, entry.url %></li>
10: <% end %>
11: <ul>
I have no idea why. I have tried restarting the server. Am I missing something obvious?
Thanks in advance.
I'm guessing that you have a simple "everything in params is a String" problem, that could explain why someone is trying to compare a Fixnum with a String. The page and per-page values should be Fixnums but, if they come from params, they will be strings. The easiest thing to do is to add a couple to_i calls as indicated:
def index
params[:per_page] ||= 25
params[:page] ||= 1
#links = Link.order('created_at DESC').page(params[:page].to_i).per_page(params[:per_page].to_i)
#---------------------------------------------------------^^^^
#------------------------------------------------------------------------------------------^^^^
end
The query that paginate produces won't be evaluated until you try to get something from it, that's why you don't get the error until you #links.each in your template.
One of my pages keeps hitting this error:
ActionView::Template::Error (undefined method `+' for nil:NilClass):
15: <div class="look_list">
16: <% collection.each do |look| %>
17: <div class="look_book" id="<%= look.content_id %>">
18: <% thumbnail_image = (look.processing? ? "/assets/processing_placeholder.gif" : (look.image.url(:thumb) + "?#{look.updated_at.to_i}")) %>
19: <%= image_tag thumbnail_image || "/assets/processing_placeholder.gif",:class=> "look_image", :size => "118x118" %>
20: <script type="text/javascript">
21:
app/views/looks/index.html.erb:18:in `block in _app_views_looks_index_html_erb__3409922204803071014_68666020'
app/views/looks/index.html.erb:16:in `_app_views_looks_index_html_erb__3409922204803071014_68666020'
Some background, my site hit into 502 bad gateway 2 days ago. We managed to restart the site. However, it caused some connection issue with mongodb. This was resolved after we restart the DB. However, this one page keep hitting the error above. This has never happened before. Any one can help?
what is the value of look.image.url(:thumb) before line 18 executes? That is where the error is thrown, and it is the only place I am seeing a +
I'd check your data, to see if the crash caused data loss somewhere, in particular, on the data needed for that method
Try out something like this .....
<div class="look_list">
<% collection.each do |look| %>
<div class="look_book" id="<%= look.content_id %>">
<% thumbnail_image = look.image.url(:thumb) unless look.processing? %>
<% thumbnail_image.nil? ? "/assets/processing_placeholder.gif" : (look.image.url(:thumb) + "?#{look.updated_at.to_i}") %>
<%= image_tag thumbnail_image ,:class=> "look_image", :size => "118x118" %>
</div>
<% end %>
</div>
I've got the following code in my show.html.erb file:
<p>
<b>Status:</b>
<% if #server.serverUp?.to_s == "Up" %>
<% #server.update_attribute(#server.serverStatus, 'Up') %>
<span style="color: green;"> <%= #server.serverUp? %></span>
<% else %>
<span style="color: #ff0000;"> <%= #server.serverUp? %></span>
<% end %>
</p>
My main concerns is with the line <% #server.update_attribute(#server.serverStatus, 'Up') %> which breaks my app from working. This line is supposed to save the status of the Server, whether it is "Up" or "Down", and save it in 'servers' table under the 'serverStatus' column.
However, when I go to http://localhost:3000/servers/id_of_server, where 'id_of_server' is a number from 1-300, the following error message appears:
NoMethodError in Servers#show
Showing C:/SIS/app/views/servers/show.html.erb where line #18 raised:
undefined method `=' for #<Server:0x60b9358>
Extracted source (around line #18):
15: <p>
16: <b>Status:</b>
17: <% if #server.serverUp?.to_s == "Up" %>
18: <% #server.update_attribute(#server.serverStatus, 'Up') %>
19: <span style="color: green;"> <%= #server.serverUp? %></span>
20: <% else %>
21: <span style="color: #ff0000;"> <%= #server.serverUp? %></span>
I don't seem to understand the error message, as I haven't used an equal sign in the extract of code provided. Any help would be appreciated.
If you guys need any more info let me know.
Thanks in advance
Edit: While I'm at it, I just wanted to ask whether I'm not following the RoR conventions by putting that type of code into the show.html.erb file as opposed to somewhere else? Because in the same file I also have another algorithm which reads all the attributes of a model, puts it into an array, and displays only the unique values.
Thanks
You should write:
<% #server.update_attribute(:serverStatus, 'Up') %>
And yes: you should not update models in view. In your case it could cleanly live in a before_save or why not in an after_initialize callback in the model I guess.
Lastly: in Ruby, convention is snake case so server_status instead of serverStatus
I have my application setup with a few different partials working well. I have asked here how to get a partial working to show the latest entry in the kase model, but now I need to show the latest 5 entries in the kase model in a partial.
I have duplicated the show most recent one partial and it's working where I need it to but only shows the last entry, what do I need to change to show the last 5?
_recent_kases.html.erb
<% if Kase.most_recentfive %>
<h4>The most recent case reference is <strong><%= Kase.most_recentfive.jobno %></strong></h4>
<% end %>
kase.rb
def self.most_recentfive
first(:order => 'id DESC')
end
Thanks,
Danny
EDIT
def self.most_recentfive
all(:order => 'id DESC', :limit=>5)
end
If I add the above code I get the following Error Message:
NoMethodError in Dashboard#index
Showing app/views/kases/_recent_kases.html.erb where line #2 raised:
undefined method `jobno' for #<Array:0x105984c60>
Extracted source (around line #2):
1: <% if Kase.most_recentfive %>
2: <h4>The most recent case reference is <strong><%= Kase.most_recentfive.jobno %></strong></h4>
3: <% end %>
def self.most_recentfive
all(:order => 'id DESC', :limit=>5)
end
EDITED TO ADD:
Then, in your partial, to display the results, you do
<% if Kase.most_recentfive %>
<h4>The most recent five case references are
<% Kase.most_recentfive.each do |k|%>
<strong><%= link_to k.jobno, k %></strong><br />
<% end %>
</h4>
<% end %>
I'm using the will_paginate plugin and I get the following error only when I'm running on a server rather than locally:
undefined method `total_pages' for []:Array
Extracted source (around line #8):
5: <% session[:page] = params[:page] %>
6: <h2>Previous Scenario</h2>
7: <% end %>
8: <%= will_paginate #scenarios, :next_label => 'Older', :prev_label => 'Newer' %>
9: <div class="box">
10: <% for scenario in #scenarios %>
11: <% #created = scenario.created_at %>
Any ideas?
Somehow, #scenarios is an ordinary Array for you and it can't be from Scenario.paginate() method because that one always returns a WillPaginate::Collection object.
Does your controller have the other half of the equation, e.g.
#scenario = Scenario.paginate(:page => params[:page]||1)
Alternatively I think you might have a plugin on the server side that is converting your Active Record set into a plain array. I'd need a bit more info to look at that.