routes

I believe I messed up the routes in one of my models.

Should I delete the model and try again?

Thanks,

Joe

Routes do not belong to a model, they map request pathes to actions in controllers, so can you please go into a little bit more detail?

As Nobert says they are independent. Post your routes.rb, where the view is you want the route to go to and say what you were expecting.

Ruby on Rails Guides: Rails Routing from the Outside In may help.

Also if you have not done so do the Getting started guide on the rails website. See it through to the end. I got confused half way when I did it but kept with it and its a great foundation.

I myself have a habit of just diving in and doing stuff. Knowing the conventions rails has and the rails way saves a lot of time in the medium term.

Ben

Uh, any particular reason to post a link to a guide for a version of Rails that's been unsupported for a very long time?

Ineptitude;):

Should be Rails Routing from the Outside In — Ruby on Rails Guides.

Ben

ok, it looks like my other form


<%= simple_form_for @gallery do |f| %>
    <% if @gallery.errors.any? %>
        <div id="error_explanation">
          <h2>
            <%= "#{pluralize(@gallery.errors.count, "error")} prohibited this image from being saved:" %>
          </h2>
          <ul>
            <% @gallerys.errors.full_messages.each do |msg| %>
                <li>
                  <%= msg %>
                </li>
            <% end %>
          </ul>
        </div>
    <% end %>



this is the error i'm getting...

NoMethodError in Gallery#new

Showing */Users/joeguerra/RubymineProjects/portfolio/app/views/gallery/_form.html.erb* where line **#12** raised:

undefined method `galleries_path’ for #<#Class:0x007feecb78ac50:0x007feec7571aa8> Did you mean? gallery_path

Extracted source (around line **#12**):

10 11 12 13 14 15

<%= simple_form_for @gallery do |f| %>

<% if @gallery.errors.any? %>

Trace of template inclusion: app/views/gallery/new.html.erb

I’m using… in my routes file.


get resources :gallery



Thanks,
Joe



You do have this in your routesfile literally? Seems invalid for me.

Also As far as I can remember the convention, `resources` needs the plural name, not the singular name. But I haven't used rails since about 3.x-ish.

RIght, I tried resources galleries, resources gallerys

But all come up with other errors. My post controller and views work, this gallery controller seems to look the same? (I don’t get it).

Thanks,

Joe

Doesn’t running the scaffold command put the route in the routes file for you?

RIght, I tried resources galleries, resources gallerys

But all come up with other errors. My post controller and views work, this gallery controller seems to look the same? (I don't get it).

rails routes will show you all configured routes, which may help

Colin

yes, I know that. :slight_smile:

But you did not understand… If you had shown the output, it might be easier for us to see what exactly went wrong.

+1

post output from ‘$ rails routes’, it will really help people help you.

also 'NoMethodError in Gallery#new sounds like the new method is missing from the Gallery controller so posting that would also be good.

Ben

Thanks, I’ll add the post the results of my routes tonight. (when I’m at home).

Why is the 'get' there?

Colin

here’s the output of my routes… (sorry about the formatting)

Prefix Verb URI Pattern Controller#Action

androids GET /androids(.:format) androids#index

POST /androids(.:format) androids#create

new_android GET /androids/new(.:format) androids#new

edit_android GET /androids/:id/edit(.:format) androids#edit

android GET /androids/:id(.:format) androids#show

PATCH /androids/:id(.:format) androids#update

PUT /androids/:id(.:format) androids#update

DELETE /androids/:id(.:format) androids#destroy

new_admin_session GET /admins/sign_in(.:format) devise/sessions#new

admin_session POST /admins/sign_in(.:format) devise/sessions#create

destroy_admin_session DELETE /admins/sign_out(.:format) devise/sessions#destroy

new_admin_password GET /admins/password/new(.:format) devise/passwords#new

edit_admin_password GET /admins/password/edit(.:format) devise/passwords#edit

admin_password PATCH /admins/password(.:format) devise/passwords#update

PUT /admins/password(.:format) devise/passwords#update

POST /admins/password(.:format) devise/passwords#create

cancel_admin_registration GET /admins/cancel(.:format) devise/registrations#cancel

new_admin_registration GET /admins/sign_up(.:format) devise/registrations#new

edit_admin_registration GET /admins/edit(.:format) devise/registrations#edit

admin_registration PATCH /admins(.:format) devise/registrations#update

PUT /admins(.:format) devise/registrations#update

DELETE /admins(.:format) devise/registrations#destroy

POST /admins(.:format) devise/registrations#create

ckeditor /ckeditor Ckeditor::Engine

root GET / posts#index

posts GET /posts(.:format) posts#index

POST /posts(.:format) posts#create

new_post GET /posts/new(.:format) posts#new

edit_post GET /posts/:id/edit(.:format) posts#edit

post GET /posts/:id(.:format) posts#show

PATCH /posts/:id(.:format) posts#update

PUT /posts/:id(.:format) posts#update

DELETE /posts/:id(.:format) posts#destroy

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

android_index GET /android(.:format) android#index

POST /android(.:format) android#create

GET /android/new(.:format) android#new

GET /android/:id/edit(.:format) android#edit

GET /android/:id(.:format) android#show

PATCH /android/:id(.:format) android#update

PUT /android/:id(.:format) android#update

DELETE /android/:id(.:format) android#destroy

Routes for Ckeditor::Engine:

pictures GET /pictures(.:format) ckeditor/pictures#index

POST /pictures(.:format) ckeditor/pictures#create

picture DELETE /pictures/:id(.:format) ckeditor/pictures#destroy

attachment_files GET /attachment_files(.:format) ckeditor/attachment_files#index

POST /attachment_files(.:format) ckeditor/attachment_files#create

attachment_file DELETE /attachment_files/:id(.:format) ckeditor/attachment_files#destroy

I asked before, but that wasn't heard, perhaps increasing the noise might help?

I’m removing most of the “noise” from your output and focussing on the gallery-relevant bits:

here’s the output of my routes… (sorry about the formatting)

Prefix Verb URI Pattern Controller#Action

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

the key bit to notice is that the index route is “named” gallery_index because you used:

resources :gallery

and :gallery is the singular form

You very likely want to say instead:

resources :galleries

which will give you similar routes except from the index:

[ruby-2.4.1p111] //tmp $ cd example/

[ruby-2.4.1p111] tmp/example (master #%) $ rails g resources Gallery

Running via Spring preloader in process 61722

Could not find generator ‘resources’. Maybe you meant ‘resource’, ‘resource_route’ or ‘assets’

Run rails generate --help for more options.

[ruby-2.4.1p111] tmp/example (master #%) $ rails g resource Gallery

Running via Spring preloader in process 61823

  invoke  active_record

  create    db/migrate/20170912230824_create_galleries.rb

  create    app/models/gallery.rb

  invoke    test_unit

  create      test/models/gallery_test.rb

  create      test/fixtures/galleries.yml

  invoke  controller

  create    app/controllers/galleries_controller.rb

invoke erb

  create      app/views/galleries

  invoke    test_unit

  create      test/controllers/galleries_controller_test.rb

invoke helper

  create      app/helpers/galleries_helper.rb

  invoke      test_unit

invoke assets

  invoke      coffee

  create        app/assets/javascripts/galleries.coffee

invoke scss

  create        app/assets/stylesheets/galleries.scss

  invoke  resource_route

   route    resources :galleries

[ruby-2.4.1p111] tmp/example (master #%) $ cat config/routes.rb

Rails.application.routes.draw do

resources :galleries

For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

end

[ruby-2.4.1p111] tmp/example (master #%) $ rails routes

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

Note the routes for index, create, and new ^^

-Rob

Thanks. What should I do to fix it? Should I re-run the generator?

Thanks. What should I do to fix it? Should I re-run the generator?

You could start by trying Rob's suggestion.

Colin