Hi I'm playing with Rails at the moment and building a basic app. When I try to run the app I get this error:
"undefined method `products_path' for #<#:0x45c19f8>"
My code is as follows...
Config:
Depot::Application.routes.draw do
resources :product
resources :test
end
Controller:
class ProductController < ApplicationController
def new
#product = Product.new
end
def show
#product = Product.find(params[:id])
end
end
View:
<h1>Page to add new products</h1>
<%= form_for(#product) do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :description %>
<%= f.text_field :description %>
<%= f.label :price %>
<%= f.text_field :price %>
<%= f.submit "Create new product" %>
<% end %>
I don't understand why the form won't render and I receive the error message. Am I missing something?
Thanks any help appreciated.
Edited to add config file.
Just add the following line in config/routes.rb
resources :products
Related
I am currently doing a CRUD, i am now in part of create method but I'm encountering when I load the new method(this is where my form is) NameError in Products#new
Question: Is my products_create_path correct? This is the action after I send the form into create method
New file
Add New Item
<%= form_for :product, url: products_create_path do |f| %>
<p>
<%= f.label :Name %><br>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :Size %><br>
<%= f.text_area :description %>
</p>
<p>
<%= f.label :Price %><br>
<%= f.text_field :price %>
</p>x
<p>
<%= f.submit :Submit %>
</p>
<% end %>
<%= link_to 'BACK', products_path %>
Routes
Rails.application.routes.draw do
resources :products
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
Controller
class ProductsController < ApplicationController
def index
#product = Product.all.order('created_at DESC')
end
def show
#post = Product.find(params[:id])
end
def new
#product = Product.new
end
def create
#post = Product.new(post_params)
if #post.save
redirect_to (products_path)
else
redirect_to('new')
end
end
private
def post_params
params.require(:product).permit(:name, :size, :price)
end
end
You don't need to give url in form_for tag. Rails internally redirect to path depends on in presence of id value.
Change form_for tag to this:
<%= form_for #product do |f| %>
So in your form_for tag, object (#product) has value for id field, then rails will submit the form to update routes else it will submit the form to create routes.
In your form change :description to :size
and change
<%= form_for :product, url: products_create_path do |f| %>
to
<%= form_for #product do |f| %>
products_create_path is not correct and you don't need it either. Your form should be <%= form_for(#product) do |f| %>
It should be:
<%= form_for :product, url: products_path do |f| %>
You can check your routes by using this command:
rake routes
Or find more information from here.
Hope I can help.
change your form_tag to
<%= form_for #post, :url => new_product_path do |f| %>
or
<%= form_for #product do |f| %>
A newby to rails (I am building an app to learn rails) and run in to an issue I can't find a solution to (while following the getting started guide). I have studied the guides and similar questions
This is my code:
class ProjecttypesController < ApplicationController
def index
#projecttypes = Projecttype.all
end
def show
#projecttype = Projecttype.find(params[:id])
end
def new
end
def create
#projecttype = Projecttype.new(projecttype_params)
#projecttype.save
redirect_to #projecttype
end
private
def projecttype_params
params.require(:projecttype).permit(:name, :image, :url)
end
end
The form:
<%= form_for :projecttypes, url: projecttypes_path do |f| %>
<p>
<%= f.label 'Project type' %>
<%= f.text_field :projecttype %>
</p>
<p>
<%= f.label :name %>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :image %>
<%= f.file_field :image %>
</p>
<p>
<%= f.label :url %>
<%= f.url_field :url %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
What am I doing wrong?
Perhaps important... when I use this...
def create
render plain: params[:projecttype].inspect
end
It returns 'nil'.
Thanks for your help
Your code should be like this
def new
#projecttype = Projecttype.new
end
def create
#projecttype = Projecttype.create(projecttype_params)
redirect_to #projecttype
end
and use this for form
<%= form_for #projecttype do |f| %>
In your
controller file
def new
#projecttype = Projecttype.new
end
and then in your form
<%= form_for #projecttype do |f| %>
I'm trying to create #booking and #booking.build_passenger in form_for with nested attributes in Rails 4.2.1
The error I get:
As you see in the console at the bottom of the image:
1. params.require(:booking) returns a Hash-like params for #booking
2. params.class returns ActionController::Parameters
As the params seems to behave correctly, IMO the problem hides somewhere in the form:
<%= form_for #booking do |f| %>
<%= f.hidden_field :flight_id, value: params[:flight_id] %>
<%= render 'flights/flight_info' %>
<div class="field">
<b><%= f.label :num_tickets, "Tickets" %></b>
<%= f.select(:num_tickets, #num_tickets) %>
</div><br>
<h4>Passenger info:</h4>
<%= f.fields_for #booking.build_passenger do |pass| %>
<div class="field">
<%= pass.label :name %>
<%= pass.text_field :name %>
</div>
<div class="field">
<%= pass.label :email %>
<%= pass.email_field :email %>
</div>
<% end %>
<%= f.submit 'Book Flight!' %>
<% end %>
Booking model:
class Booking < ActiveRecord::Base
belongs_to :flight
belongs_to :passenger
accepts_nested_attributes_for :passenger
end
Question: Where and how do I have to edit my code for the app to start creating #booking instances + #booking.build_passenger()
Your booking_params needs to be something like:
def booking_params
params.require(:booking).permit(:flight_id, :num_tickets, passenger_attributes: [:id, :name, :email])
end
I'm writing a simple web application which I like to call PMS (Projects Management System). It this app I have 2 models Projects and Students. I have set (I guess... cuz I'm a newbie) association between these two models. Projects has many students but student belongs to one project (Maybe it'll change with time).
But my problem is in getting everything work together. I don't know how I can insert new students inside new project form. I've tried everything and still nothing!
Here are my source files:
Projects controller:
class ProjectsController < ApplicationController
def show
#projects = Project.all
end
def create
#project = Project.new(project_params)
#project.status = "Waiting"
#project.save
redirect_to root_path
end
private
def project_params
params.require(:project).permit(:title, :lecturer)
end
end
Students Controller:
class StudentsController < ApplicationController
def create
#project = Project.find(params[:project_id])
#student = #project.students.create(params[:student])
#student.save
end
end
Models:
class Project < ActiveRecord::Base
has_many :students
end
class Student < ActiveRecord::Base
belongs_to :project
end
View:
Add new project
<%= form_for :project, url: projects_path do |f| %>
<p>
<%= f.label :title %>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :lecturer %>
<%= f.text_field :lecturer %>
</p>
<%= form_for([#project, #project.students.build]) do |s| %>
<p>
<%= s.label :name %><br />
<%= s.text_field :name %>
</p>
<% end %>
<p>
<%= f.submit %>
</p>
<% end %>
Routes:
RoRPMS::Application.routes.draw do
# You can have the root of your site routed with "root"
root 'projects#show'
resources :projects do
resources :students
end
end
You can use "nested_form" to create project with students as well
<%= nested_form_for #project do |f| %>
<p>
<%= f.label :title %>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :lecturer %>
<%= f.text_field :lecturer %>
</p>
<%= fields_for :students do |s| %>
<p>
<%= s.label :name %><br />
<%= s.text_field :name %>
</p>
<% end %>
<%= f.link_to_add "Add new student", :students %>
<p>
<%= f.submit %>
</p>
<% end %>
In Project Model add
accepts_nested_attributes_for :students
I have this controller
class PeopleController < ApplicationController
def new
#person = Person.new
#person.phones.new
end
# this is the action that gets called by the form
def create
render text: person_params.inspect
# #person = Person.new(person_params)
# #person.save
# redirect_to people_path
end
def index
#person = Person.all
end
private
def person_params
params.require(:person).permit(:name, phones_attributes: [ :id, :phone_number ])
end
end
and this view
<%= form_for :person, url: people_path do |f| %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<%= f.fields_for :phones do |f_phone| %>
<div class="field">
<p>
<%= f_phone.label :phone_number %><br />
<%= f_phone.text_field :phone_number %>
</p>
</div>
<% end %>
<p>
<%= f.submit %>
</p>
<% end %>
When I fill out both form fields and hit "Save Person" I only get {"name"=>"foo"} - the phone number seems to vanish.
However, when I change phones_attributes to phones I get {"name"=>"foo", "phones"=>{"phone_number"=>"123"}} (this would however cause problems with the create function.
What's wrong here?
Please note that this question is strongly related to that one: accepts_nested_attributes_for: What am I doing wrong as well as to this posting: https://groups.google.com/forum/#!topic/rubyonrails-talk/4RF_CFChua0
You don't have #phones defined in the controller:
def new
#person = Person.new
#phones = #person.phones.new
end
Finally found the problem. In the view there should be
<%= form_for #person, url: people_path do |f| %>
Instead of
<%= form_for :person, url: people_path do |f| %>
#phron said that already here:
accepts_nested_attributes_for: What am I doing wrong