jhgju77
jhgju77

Reputation: 1151

rails undefined method on form

i have model User and Gallery

user has many gallies and gallery belongs to User

models:

class User < ApplicationRecord
   has_many :galleries
end

class Gallery < ApplicationRecord
  has_many_attached :files
  belongs_to :user
end

on my controller :

class GalleryController < ApplicationController

  def new
    @gallery = Gallery.new
  end

end

and then on gallery/new.html.haml :

.container
  %h4
    New Gallery
  %div
    = form_for @gallery do |f|
      .form-group
        = f.label :title, class: 'col-md-2 col-form-label'

then when i go to localhost:3000/gallery/new it said :

undefined method `galleries_path' for #<#<Class:0x00007f9f4e865908>:0x00007f9f4ae31b10>
Did you mean?  gallery_path

but when i command that for_for and change it to some text Hellow word, the page was just fine, is there something i missing here ??

i am using ruby 2.7.1 and latest Rails full error here

routes:

                      gallery_index GET    /gallery(.:format)                                                                       gallery#index
                                      POST   /gallery(.:format)                                                                       gallery#create
                          new_gallery GET    /gallery/new(.:format)                                                                   gallery#new
                         edit_gallery GET    /gallery/:id/edit(.:format)                                                              gallery#edit
                              gallery GET    /gallery/:id(.:format)                                                                   gallery#show
                                      PATCH  /gallery/:id(.:format)                                                                   gallery#update
                                      PUT    /gallery/:id(.:format)                                                                   gallery#update
                                      DELETE /gallery/:id(.:format)                                                                   gallery#destroy

Upvotes: 0

Views: 171

Answers (1)

max
max

Reputation: 101811

The problem is that you called resources :gallery and not resources :galleries. Which generates the wrong routes:

                  gallery_index GET    /gallery(.:format)  gallery#index

The argument to resources should always be the plural form.

# config/routes.rb
Rails.application.routes.draw do
  resources :galleries
end

                           Prefix Verb   URI Pattern                                                                              Controller#Action
                        galleries GET    /galleries(.:format)                                                                     galleries#index
                                  POST   /galleries(.:format)                                                                     galleries#create
                      new_gallery GET    /galleries/new(.:format)                                                                 galleries#new
                     edit_gallery GET    /galleries/:id/edit(.:format)                                                            galleries#edit
                          gallery GET    /galleries/:id(.:format)                                                                 galleries#show
                                  PATCH  /galleries/:id(.:format)                                                                 galleries#update
                                  PUT    /galleries/:id(.:format)                                                                 galleries#update
                                  DELETE /galleries/:id(.:format)                                                                 galleries#destroy

Your controller name should also be plural:

# app/controllers/galleries_controller.rb
class GalleriesController < ApplicationController
  # GET /galleries/new
  def new
    @gallery = Gallery.new
  end
end

As should the views folder:

$ mv app/views/gallery app/views/galleries 

Upvotes: 4

Related Questions