0

I have a routing error with a nested resource. Here is my nested routing:

resources :users do
  resources :pages
end

This is my minitest "visit new user page" system test:

test "visit new user page path" do
  user = User.create
  visit new_user_page_path(user)
  assert_selector "h1", text: "Page"
end

This fails with the following error:

Error:
PagesTest#test_visit_new_user_page_path:
ActionView::Template::Error: undefined method `pages_path' for #<#<Class:0x00007fa0299dfa28>:0x00007fa02aa23df8>
Did you mean?  image_path
app/views/pages/_form.html.erb:1:in `_app_views_pages__form_html_erb__3658586168370814469_70162960780560'
app/views/pages/new.html.erb:3:in `_app_views_pages_new_html_erb__3548077654233884011_70162934875400'

I realize that pages_path is not a correct path for this nested resource. The correct path to pages#new is new_user_page_path(@user) (which is the path that took me to new.html.erb). The correct path to pages#create is a POST to user_pages_path(@user) (which is the page that new.html.erb should POST to). But I cannot find anywhere that pages_path is being called. The error says that it is being called in new.html.erb and also _form.html.erb. Here are those pages. First, new.html.erb:

<h1>New Page</h1>

<%= render 'form', page: @page %>
<%= link_to 'Back', user_pages_path(@page) %>

And _form.html.erb:

<%= form_with(model: page, local: true) do |form| %>
  <% if page.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(page.errors.count, "error") %> prohibited this page from being saved:</h2>

      <ul>
      <% page.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= form.label :title %>
    <%= form.text_field :title, id: :page_title %>
  </div>

  <div class="field">
    <%= form.label :content %>
    <%= form.text_area :content, id: :page_content %>
  </div>

  <div class="field">
    <%= form.label :user_id %>
    <%= form.text_field :user_id, id: :page_user_id %>
  </div>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

I don't know where pages_path is being called, so I can't fix that error. Any help is appreciated.

1 Answer 1

1

You need to change your form code, page_path is called from it. Should be

<%= form_with(model: [ @user, @page ]) do |form| %>

In this case the route will be set correctly.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Vasilisa. I was so focused on thinking it was a routing problem, I did not realize it was really a form helper problem. Ugh! ;-(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.