k1tsunee
k1tsunee

Reputation: 3

Ruby on Rails undefined method `referrals_path'

I'm getting the following error when trying to do a form write data on my database (sqlite3)

undefined method `referrals_path' for #<ActionView::Base:0x00000000008cc8>

My routes.rb

Rails.application.routes.draw do
...
  post "afiliados", to: "afiliados#create"
...
end

My afiliados_controller.rb

class AfiliadosController < ApplicationController
...
def create
    cliente = params.require(:referral).permit(:nomeAfiliado, :nomeReferenciado, :comissao, :validadeComissao)
    Referral.create cliente
  end
end

My cadastroTeste.html.erb

<html>
    <body>
        <a href="/afiliados">Voltar</a>
        <%= form_for Referral.new do |form| %>
            Nome <input type="text" name="referral[nomeAfiliado]"><br/>
            Nome Referenciado <input type="text" name="referral[nomeRef]"><br/>
            Comissão <input type="number" name="referral[comissao]"><br/>
            Válido até <input type="date" name="referral[validadeRef]"><br/>
            <button type="submit">Criar</button>
        <% end %>
    </body>
</html>

I am following a tutorial from Alura (online programming school) and don't know what to do. I've tried changing my routes.rb to referral#create, but didn't worked.

Upvotes: 0

Views: 21

Answers (1)

Deepesh
Deepesh

Reputation: 6398

I am not sure about the tutorial what it has but you are getting this error because of:

<%= form_for Referral.new do |form| %>

which expects a ReferralController and a route defined to its create action. You are trying to let Rails detect the controller and action automatically using its conventions on the form but you are not using the conventions on the controller. By this statement, I mean that the Referral object should be created on the ReferralController create action instead of AfiliadosController.

If you still want to make this work you could do:

<%= form_for afiliados_path do |form| %>

You could read more about this here: https://guides.rubyonrails.org/form_helpers.html#dealing-with-model-objects

Upvotes: 1

Related Questions