Nomethoderror in Usuario#new Rubyonrails

Asked

Viewed 108 times

1

controller:

class UsuarioController < ApplicationController

    def index
        @usuarios = Usuario.order :nome
    end

    def new 
        @usuario = Usuario.new
    end

    def show
        @usuario = Usuario.find(params[:id])
    end

    def destroy
        @usuario = Usuario.find(params[:id])
        @usuario.destroy

        redirect_to(action: "index")
    end

end

View:

New User

<%= form_for @usuario do |f| %>
  Nome: <%= f.text_field :nome %><br/>
<% end %>

Error:

  NoMethodError in Usuario#new
Showing /vagrant/crudexemplo/app/views/usuario/new.html.erb where line #4 raised:

undefined method `usuarios_path' for #<#<Class:0x007fce441b1e20>:0x007fce31f1e148>
  • Do the following check the routes using rake Routes and then glue here for us to look. rake Routes

1 answer

1


You need to add the route in config/Routes.Rb

Sample::Application.routes.draw do
  #outras rotas...

  resource :usuario #adicione essa linha
end

and controller name always in plural ex.: User

  • Dude I’ve already added :|

  • Rails.application.Routes.draw do Resources :user root 'user#index' get '/login' => 'user#index'

  • is "user" and not "user"

  • but because it has to be plural ?

  • Didn’t work ..

  • Resources for plural and Resource for singular Ex.: Resources :users or Resource :user controller name is singular so use Resource* instead of Resources*

  • normally if you use the controller name in the plural, you can put it in the plural and do as I had done before, or simply use the singular route

  • I have no reputation to chat with..

  • 1

    I renamed the controller and it worked .

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.