Rails nested routes undefined method error

While working on belt-tracker I had an issue with nested routes and using the

form_for

tag provided by Rails. Every time I tried loading the form I was getting an error:

no method found `lessons_path`

I had :lessons as a nested route in my gym routes and that was causing a pathing error but I was struggling with getting the lesson to assign to the gym using the correct ID. I ended up using a hidden field to store the gym_id while sending the form and moving the :lessons resources to be both nested and not nested, which fixed the pathing errors.

New Lesson Form:

<%= form_for(@gym.lessons.new) do |f| %>
  <%= f.label "Name" %>
  <%= f.text_field :name %>
  <%= f.label "Length", "Length in hours (eg: 1, 0.5, 1.5)" %>
  <%= f.text_field :length %>
  <%= f.hidden_field :gym_id, value: params[:gym_id] %>
  <%= f.submit %>
<% end %>

About Nick

Melbourne based web developer that loves building front-end systems with anything from vanilla JS to React. Infosec student and active learner. Subscribe to the blog

www ·

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.