Reputation:
Rails 5.1.3
I have a namespaced set of routes in my route file within rails:
Rails.application.routes.draw do
get 'page/index'
namespace :admin do
resources :pages
resources :sections
end
get '*page', to: 'page#index'
root 'page#index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
I then have a pages controller:
class Admin::PagesController < ApplicationController
def new
render layout: 'admin'
end
def index
@pages = Page.all
render layout: 'admin'
end
def edit
@page = Page.find(params[:id])
render layout: 'admin'
end
end
As you can see I have defined my edit method in which I get the page for the corresponding ID (fairly typical).
Within my HTML I then have:
<h1>Edit page</h1>
<%= form_with(model: @page) do |form| %>
<p>
<%= form.label :slug %><br>
<%= form.text_field :slug %>
</p>
<p>
<%= form.label :title %><br>
<%= form.text_area :title %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
As per the rails guide, but I get the error:
undefined method `page_path' for #<#> :0x007fbdfd15f000> Did you mean? image_path Extracted source (around line #3):
Edit page
<%= form_with(model: @page) do |form| %>
<%= form.label :slug %>
I suspect it is due to the namespace, how can I resolve this?
Upvotes: 2
Views: 1322
Reputation: 7167
You are right, it's about the namespace:
<%= form_with(model: [ :admin, @page ]) do |form| %>
Upvotes: 2
Reputation: 3578
I do not see where you've defined the route for 'page_path' in your routes.
You've defined 'admin_page_path'.
Use 'rails routes' to see exactly how your routes are mapped out.
Also, in your controller, you can use 'layout "admin"' after your class definition to use that layout for all actions.
Upvotes: 1
Reputation: 6455
You've not named your route. Try:
get 'page/index', as: 'page'
If you run rake routes from your terminal, it gives you an extremely helpful breakdown of all your routes names, params and namespaces
Upvotes: 0