0

So I did find similar questions but could not quite find a good answer, so here's my question

I keep getting this method error when trying to go into localhost:3000/clients/1/precios/new (the error comes from the _form.html.erb)

First of all routes.rb

Rails.application.routes.draw do
  
  #resources :precios
  #resources :catalogos
  resources :clients do
    #resources :catalogs, shallow: true
    resources :remissions, shallow: true 
    resources :catalogos
    resources :precios
    
  end

  resources :catalogs do 
      resources :products, shallow: true do
        resources :prices, shallow: true
      end
    end
  

  devise_for :users

  devise_scope :user do
    get '/users/sign_out  ' => 'devise/sessions#destroy'
  end

  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
  root 'home#index', as: 'home'

  get 'new' => 'clients#new', as: 'alta_cliente'
  
  get 'clients#index' => 'clients#index', as: 'lista_clientes'

  get 'remissions#index' => 'remissions#new', as: 'nueva_remision'
end

And this is the error I keep getting

undefined method `precios_path' for #<#<Class:0x000000000d1f7018>:0x000000000a18fc90>
Did you mean?  price_path

Extracted source (around line #2):
<div class="alta-form-container">
  <%= form_with(model: precio, local: true) do |form| %> (Error in this line)
    <% if precio.errors.any? %>
      <div id="error_explanation">
        <h2><%= pluralize(precio.errors.count, "error") %> prohibited this precio from being saved:</h2>

And this is my _form.html.erb

<div class="alta-form-container">
  <%= form_with(model: precio, local: true) do |form| %>
    <% if precio.errors.any? %>
      <div id="error_explanation">
        <h2><%= pluralize(precio.errors.count, "error") %> prohibited this precio from being saved:</h2>

        <ul>
          <% precio.errors.full_messages.each do |message| %>
            <li><%= message %></li>
          <% end %>
        </ul>
      </div>
    <% end %>

    <div class="field">
      <%= form.label :precio %>
      <%= form.number_field :precio %>
    </div>

    <div class="field">
      <%= form.label :client_id %>
      <%= form.text_field :client_id %>
    </div>

    <div class="field">
      <%= form.label :catalogo_id %>
      <%= form.text_field :catalogo_id %>
    </div>

    <div class="actions">
      <%= form.submit %>
    </div>
  <% end %>
</div>

What is it that I need to change in the form?

1 Answer 1

3

You have following code in routes

resources :clients do
    resources :precios
end

So you need both clients as well as precios object, try following

<%= form_with(model: [client, precio], local: true) do |form| %>

Note:- You should have access to the client object in _form.html.erb

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.