Nick Seferos
Nick Seferos

Reputation: 124

Rails 6 - Calling incorrect helper

Today my app began to call the incorrectly named helper.

When loading the views for Drills model, Exercise models helper is called. This started happening today without changing anything.

For example, when I load my show view for Drills:

  Rendering drills/show.html.erb within layouts/application
  Rendered drills/show.html.erb within layouts/application (Duration: 3.5ms | Allocations: 1551)
Completed 500 Internal Server Error in 63ms (ActiveRecord: 2.8ms | Allocations: 6965)


  
ActionView::Template::Error (undefined method `created_at' for nil:NilClass):
    3:     <div class="flex justify-between items-center mb-4">
    4:       <h1 class="h3"><%= link_to 'Drills', drills_path %> > Drill on <%= @drill.date_held.strftime('%d/%b/%y') %></h1>
    5:       <%= link_to 'Download PDF', drill_path(@drill, :pdf), class: "btn btn-link" %>
    6:       <% if can_edit? %><%= link_to 'Edit', edit_drill_path(@drill), class: "btn btn-link" %><% end %>
    7:       <% if can_destroy? %><%= link_to 'Delete', drill_path, class: "btn btn-danger outline", method: :delete, data: { remote: true, confirm: "Are you sure?" } %> <% end%>
    8:     </div>
    9: 
  
app/helpers/exercises_helper.rb:6:in `can_edit?'
app/views/drills/show.html.erb:6

The incorrect controller is being called? How is this fixed?

Upvotes: 0

Views: 179

Answers (1)

jdgray
jdgray

Reputation: 1983

Check out the documentation about Rails helpers: https://api.rubyonrails.org/classes/ActionController/Helpers.html

In addition to using the standard template helpers provided, creating custom helpers to extract complicated logic or reusable functionality is strongly encouraged. By default, each controller will include all helpers. These helpers are only accessible on the controller through #helpers

It appears that you already have a method called can_edit? in your exercises_helper.rb.

If you want the Drill helpers only in the Drill controller/views then you will have to follow this:

To return old behavior set config.action_controller.include_all_helpers to false.

Upvotes: 3

Related Questions