Rails Best Practices is telling me to move some code from one of my
helper views into the controller. I was hoping to get some assistance as
I'm unsure how to do this and still have the application work.
Yes. That's it. Do your best to keep logic out of the views. It
will make your application more testable. The selection of data sets
is one type of logic.
Bill, that doesn't seem to be working. It seemed to be at first until I
tried to do more with the application.
My shared/_task_orders.html.erb
<ul type="none">
<% @all_task_orders.each do |task_order| %>
<% unless task_order.year != @last_task_order.year %>
<li>     <%=h link_to "#{task_order.name}",
task_order_criteria_path(task_order) %> - <%=h task_order.year %> Q<%=h
task_order.quarter %></li>
<% end %>
<% end %>
</ul>
<br />
My task_orders_controller
class TaskOrdersController < ApplicationController
...
def index
@task_orders = TaskOrder.search(params[:search], params[:page])
@last_task_order = TaskOrder.find(:first)
@all_task_orders = TaskOrder.all
end
...
end
I'm getting:
NoMethodError in Criteria#index
Showing app/views/shared/_task_orders.html.erb where line #2 raised:
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
Bill, that doesn't seem to be working. It seemed to be at first until I
tried to do more with the application.
My shared/_task_orders.html.erb
<ul type="none">
<% @all_task_orders.each do |task_order| %>
<% unless task_order.year != @last_task_order.year %>
Not related to your problem, but if this is the only place you are
referencing the task_orders, and you have the test here so that you
only show some of them, you would be better to only pick up the
relevant ones in the controllers and pass that array to the view.
Then you do not need the test in the view.
task_order_criteria_path(task_order) %> - <%=h task_order.year %> Q<%=h
task_order.quarter %></li>
<% end %>
<% end %>
</ul>
<br />
My task_orders_controller
class TaskOrdersController < ApplicationController
...
def index
@task_orders = TaskOrder.search(params[:search], params[:page])
@last_task_order = TaskOrder.find(:first)
@all_task_orders = TaskOrder.all
end
...
end
I'm getting:
NoMethodError in Criteria#index
Why does that say Criteria#index when you have shown us
TasksOrderController#index?
Showing app/views/shared/_task_orders.html.erb where line #2 raised:
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
Extracted source (around line #2):
1: <ul type="none">
2: <% @all_task_orders.each do |task_order| %>
The error message is saying that @all_tasks_orders is nil. I see that
this is in a partial, have you passed the data across to the partial.
The mechanism for doing this varies with Rails version. The Rails
Guide on Layouts and Rendering discusses the various methods for
getting data to partials. The other possibility is of course that
@all_task_orders was nil in the first place, possibly because the
partial has been called from Criteria#index and that route has not set
@all_task_orders.
Are you aware of how to use ruby-debug to break into your code (View,
Controller or Model) in order to check code flow and inspect data to
see what is happening? Have a look at the Rails Guide on debugging to
see how. This is invaluable and I strongly suggest you have a look a
this. It will save a lot of time waiting for response from the list
if you can just break into the code and work out for yourself why
something is not working.
It's working just fine. The error message is telling you that
@all_task_orders is nil. You just have to figure out why. The fact
that it is created in the controller makes it easier to debug.
Execute the commands in your index method from the console (ruby
script/console) and see what they return.