Error of unidentified method in form - Ruby on rails - ruby-on-rails

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?

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

Related

not able to render form in rails association

i created a project scaffold that has many to one association with stage scaffold now i created a task scaffold that has many to one association with stage scffold.
but i task form is not rendered i am getting error.
routes.rb
resources :projects do
resources :stages do
resources :tasks
end
end
Task form.html.erb
<%= form_with(model: task, url: [#stages, task], local: true) do |form| %>
<% if task.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(task.errors.count, "error") %> prohibited this task from being saved:</h2>
<ul>
<% task.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field columns large-6">
<%= form.label :task_name %>
<%= form.text_field :task_name %>
</div>
<div class="actions">
<%= form.submit 'Create', :class=>'button primary small' %>
</div>
<% end %>
model project.rb
has_many :stages
model stage.rb
belongs_to :project
has_many :tasks
model task.rb
belongs_to :stage
Rails can't find tasks_path in your application, because you moved tasks under projects and stages resources, so full path to tasks_path looks like projects_stages_tasks_path
So, you need to specify this url for given projects and stages
<%= form_with(model: task, url: projects_stages_tasks_path(#stages, #stages.project), local: true) do |form| %>

is missing a template for request formats: text/html

I have Term and Phrase models and I am adding Nested Resources. I want to get the Term Id for Phraes_term to create a data pair
Example: Term 1 and Phrase 2.
But when you press the new button from show.html.erb term, an error will occur
-->error :PhrasesTermsController#new is missing a template for request formats: text/html
form.html.erb
-(phrases_term).
<%= form_with(model: phrases_term, local: true) do |form| %>
<% if pharases_term.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(pharases_term.errors.count, "error") %> prohibited this phrase from being saved:</h2>
<ul>
<% pharases_term.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :ID %>
<%= form.number_field_tag :ID %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
(phrases_term)
-new.html.erb
<%= render 'form', phrases_term: #phrases_term %>
Phrases_terms_controller.rb
class PhrasesTermsController < ApplicationController
before_action :authenticate_user!
before_action :set_term
def new
#phrases_term = PhrasesTerm.new
end
def create
#phrases_term = #term.phrases_term
#phrases_term.user = current_user
#phrases_term.save
redirect_back(fallback_location: root_path)
end
private
def phrases_term_params
params.require(:phrases_term).permit(:term_id)
end
def set_term
#term = Term.find(params[:term_id])
end
end
routes.rb
resources :terms do
resources :phrases_terms, only: [:create, :destroy, :new]
end
(Term)
Show.html.erb
<td><%= link_to 'New', new_term_phrases_term_path(#term) %></td>
(phrases_term).
_form.html.ebr
<%= form_with(model: phrases_term, local: true) do |form| %>
<% if pharases_term.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(pharases_term.errors.count, "error") %> prohibited this phrase from being saved:</h2>
<ul>
<% pharases_term.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :ID %>
<%= form.number_field_tag :ID %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
(phrases_term)
new.html.erb
<%= render 'form', phrases_term: #phrases_term %>
This means you do not have a app/views/phrases_term(s)/new.html.erb file. I'm unsure if you've namespaced the files under 'phrases_term' or 'phrases_terms'.
Make one in the correct directory, populate it, and it will load.

Rails nested resource routing error

I have a routing error with a nested resource. Here is my nested routing:
resources :users do
resources :pages
end
This is my minitest "visit new user page" system test:
test "visit new user page path" do
user = User.create
visit new_user_page_path(user)
assert_selector "h1", text: "Page"
end
This fails with the following error:
Error:
PagesTest#test_visit_new_user_page_path:
ActionView::Template::Error: undefined method `pages_path' for #<#<Class:0x00007fa0299dfa28>:0x00007fa02aa23df8>
Did you mean? image_path
app/views/pages/_form.html.erb:1:in `_app_views_pages__form_html_erb__3658586168370814469_70162960780560'
app/views/pages/new.html.erb:3:in `_app_views_pages_new_html_erb__3548077654233884011_70162934875400'
I realize that pages_path is not a correct path for this nested resource. The correct path to pages#new is new_user_page_path(#user) (which is the path that took me to new.html.erb). The correct path to pages#create is a POST to user_pages_path(#user) (which is the page that new.html.erb should POST to). But I cannot find anywhere that pages_path is being called. The error says that it is being called in new.html.erb and also _form.html.erb. Here are those pages. First, new.html.erb:
<h1>New Page</h1>
<%= render 'form', page: #page %>
<%= link_to 'Back', user_pages_path(#page) %>
And _form.html.erb:
<%= form_with(model: page, local: true) do |form| %>
<% if page.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(page.errors.count, "error") %> prohibited this page from being saved:</h2>
<ul>
<% page.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :title %>
<%= form.text_field :title, id: :page_title %>
</div>
<div class="field">
<%= form.label :content %>
<%= form.text_area :content, id: :page_content %>
</div>
<div class="field">
<%= form.label :user_id %>
<%= form.text_field :user_id, id: :page_user_id %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
I don't know where pages_path is being called, so I can't fix that error. Any help is appreciated.
You need to change your form code, page_path is called from it. Should be
<%= form_with(model: [ #user, #page ]) do |form| %>
In this case the route will be set correctly.

NoMethodError in Rails when creating a new instance of model

I have a model that depends from another model, I have specified it like this in my routes.rb file
Rails.application.routes.draw do
resources :events do
resources :guests
get :whereabouts
end
devise_for :users, :controllers => { registrations: 'registrations' }
models/event.rb:
class Event < ActiveRecord::Base
belongs_to :user
has_many :guests
end
models/guest.rb:
class Guest < ActiveRecord::Base
belongs_to :event
end
When I access http://localhost:3000/events/2/guests/ it works properly but when I access http://localhost:3000/events/2/guests/new I get
undefined method `guests_path' for #<#<Class:0x00000004074f78>:0x0000000aaf67c0>
from line #1 of my views/guests/_form.html.erb file which is
<%= form_for(#guest) do |f| %>
<% if #guest.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#guest.errors.count, "error") %> prohibited this event from being saved:</h2>
<ul>
<% #guest.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.text_field :first_name %>
</div>
<div class="field">
<%= f.text_field :last_name %>
</div>
<div class="field">
<%= f.text_field :email %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I changed the paths to the proper ones since now guests_path should be event_guests_path, but I don't know where to change it in my form for it to work properly.
Any clue? Is my routing wrong?
Your routing is correct. But the guest object depends from the event one so you have to change the form to:
<%= form_for [#event, #guest] do |f| %>
...
<% end %>
you must have initialized the #event variable in the controller where you probably did something like:
#event = Event.find(params[:event_id])
#guest = #event.guests.build
From the docs:
For namespaced routes, like admin_post_url:
<%= form_for([:admin, #post]) do |f| %>
...
<% end %>
So, in your case, you probably want this:
<%= form_for([:event, #guest]) do |f| %>

undefined method `twitter_source_twitter_aggregation_methods_path' for #<#<Class:0xb982e10>:0xc104ef4>

I have 3 nested resources: sources, twitter_source and twitter_aggregation_methods.
The relationship of their respective models is as follows:
sources.rb
has_many :twitter_sources
has_many :rss_sources
twitter_source.rb
has_many :twitter_aggregation_methods
belongs_to :source
twitter_aggregation_method.rb
belongs_to :twitter_source
The config/routes.rb is set up as follows:
resources :sources do
resources :rss_sources
resources :twitter_sources do
resources :twitter_aggregation_methods
resources :influencer_trends do
resources :trends
end
end
end
When I try to add a new twitter_aggreggation_method I get the following error:
NoMethodError in TwitterAggregationMethods#new
Showing /home/notebook/work/abacus/app/views/twitter_aggregation_methods/_form.html.erb where line #1 raised:
undefined method `twitter_source_twitter_aggregation_methods_path' for #<#<Class:0xb982e10>:0xc104ef4>
My views/twitter_aggregation_methods/_form.html.erb is as follows:
<%= form_for([#twitter_source, #twitter_aggregation_method]) do |f| %>
<% if #twitter_aggregation_method.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#twitter_aggregation_method.errors.count, "error") %> prohibited this twitter_aggregation_method from being saved:</h2>
<ul>
<% #twitter_aggregation_method.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :twitter_source_id %><br>
<%= f.text_field :twitter_source_id, value: #twitter_source.id %>
</div>
<div class="field">
<%= f.label :aggregation_method %><br>
<%= f.text_field :aggregation_method %>
</div>
<div class="actions">
<%= f.submit %>
The error points to the first line of this partial.
Looking at the resources defined, twitter_source_twitter_aggregation_methods_path doesn't exist. Hence, the error.
You can do rake routes and check the prefixes to know what all routes exist.
From TwitterAggregationMethods#new you would be expecting to go to TwitterAggregationMethods#create, for that pass an instance of source in the form,
Replace
<%= form_for([#twitter_source, #twitter_aggregation_method]) do |f| %>
with
<%= form_for([#source, #twitter_source, #twitter_aggregation_method]) do |f| %>
and set #source in the TwitterAggregationMethods#new.

Resources