Hello, 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. The helper: pastie.org/private/pfub9iklus5ajuu1iwvfza The report says I need to move the second and third lines into the controller, but if I do that, how does the rest of the code know how to work? Thanks, Andrew Davis -- Posted via ruby-forum.com. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en.
Hi Andrew, On Wed, Nov 24, 2010 at 11:59 AM, Andrew Davis <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hello, > > 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. > > The helper: pastie.org/private/pfub9iklus5ajuu1iwvfza > > The report says I need to move the second and third lines into the > controller, but if I do that, how does the rest of the code know how to > work?Do your finds in the controller, assigning the results to instance variables, and then access the instance variables in the view. HTH, Bill -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en.
Since I''m still learning, here is my attempt, is this what you are trying to say? pastie.org/private/hiqrzql2zb4qv5gr1drpa I think I understand what you''re saying, just not sure if I''m doing that correctly. -- Posted via ruby-forum.com. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en.
On Wed, Nov 24, 2010 at 12:35 PM, Andrew Davis <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Since I''m still learning, here is my attempt, is this what you are > trying to say? > > pastie.org/private/hiqrzql2zb4qv5gr1drpa > > I think I understand what you''re saying, just not sure if I''m doing that > correctly.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. HTH, Bill -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en.
Thank you very much Bill for your assistance, it is greatly appreciated :) Andrew Davis -- Posted via ruby-forum.com. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en.
On Wed, Nov 24, 2010 at 1:03 PM, Andrew Davis <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Thank you very much Bill for your assistance, it is greatly appreciated > :)You''re welcome, Andrew. Welcome to the Rails community. Best regards, Bill -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en.
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 Extracted source (around line #2): 1: <ul type="none"> 2: <% @all_task_orders.each do |task_order| %> 3: <% unless task_order.year != @last_task_order.year %> 4: <li>     <%=h link_to "#{task_order.name}", task_order_criteria_path(task_order) %> - <%=h task_order.year %> Q<%=h task_order.quarter %></li> 5: <% end %> Thank you for helping me!! Andrew Davis -- Posted via ruby-forum.com. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en.
On 24 November 2010 19:05, Andrew Davis <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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.> <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#indexWhy 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. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en.
On Wed, Nov 24, 2010 at 2:05 PM, Andrew Davis <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Bill, that doesn''t seem to be working. It seemed to be at first until I > tried to do more with the application.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. Best regards, Bill -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en.