Cédric
Cédric

Reputation: 470

NoMethodError in Pages#new undefined method `pages_index_path'

I'm trying to generate a form using the form_for helper in RoR but I am encountering what seems to be a routing error. Here are the relevant files:

routes.rb

Rails.application.routes.draw do
  devise_for :users
  devise_for :models

  root to: 'pages#home'
  resources :pages
end

controllers/pages_controller.rb

class PagesController < ApplicationController
  def index
    @pages = Pages.all
  end

  def show
    @page = Pages.find(params[:id])
  end

  def edit
    @page = Pages.find(params[:id])
  end

  def update
    @page = Pages.find(params[:id])
    @page.update(page_params)
    redirect_to page_path
  end

  def new
    @page = Pages.new
  end

  def create
    page = Pages.create(page_params)
    redirect_to page_path(page.id)
  end

  private

  def page_params
    params.require(:pages).permit(:name, :content)
  end

end

views/pages/new.html.erb

<h1>Créer une fiche</h1>

<%= form_for @page do |g| %>
    <div class="form-group">
        <label>Titre de la fiche</label>
        <%= g.text_field :name, class: 'form-control' %>
    </div>
    <div class="form-group">
        <label>Contenu de la fiche</label>
        <%= g.text_area :content, class: 'form-control', size: "60x12" %>
    </div>
    <div class="form-group">
        <%= g.submit "Ajouter la fiche", class: 'btn btn-primary' %>
    </div>
<% end %>

<a href="<%= pages_path %>" class="btn btn-primary">Revenir à la liste des fiches</a>

I can't see what's wrong, still the outputt is :

Displayed error on Mozilla

The same form is working in views/pages/create.html.erb If I remove the form and try to directly dump @page with <%= @page %> it returns #<Pages:0x000055de2b6a2b10>

Upvotes: 1

Views: 910

Answers (1)

jaspreet-singh-3911
jaspreet-singh-3911

Reputation: 211

You can specify the url manually for the form.

<%= form_for @page, url: pages_path, method: :post do |g| %>

Upvotes: 2

Related Questions