Hello I am absolute newbie in rails. creating some application but stuck on some point.
The problem is I want to insert data in products as per user id but it is saying:
uninitialized constant ProductController
Here is my route file:
Rails.application.routes.draw do
#devise_for :users
devise_for :users, :controllers => { :registrations => 'users/registrations' }
resources :dashboard
root to: "home#index"
namespace :user do
resources :users
end
resources :product
end
here is my product controller i.e, product_controller.rb:
class Product::ProductController < ApplicationController
def new
@product = Product.new
end
def create
@product = Product.new(params[:product])
if @product.save
flash[:success] = "Product Added"
redirect_to product_index_path
else
flash[:success] = @product.errors.full_messages.join
redirect_to :back
end
end
end
And here is my new.html.erb code:
<h2>Add Product</h2>
<%= form_for(resource, :as => resource_name, :url => user_registration_path) do |f| %>
<div class="field">
<%= f.label :product_name %><br />
<%= f.text_field :product_name, autofocus: true %>
</div>
<%= p.hidden_field :user_id, :value => current_user %>
<div class="actions">
<%= f.submit "Add Product" %>
</div>
<% end %>
<% end %>
I know this thing is wrong too:
<%= form_for(resource, :as => resource_name, :url => user_registration_path) do |f| %>
How I can make all the parts to one? my goal is to take use to a new page add product name and redirect to product list page.
Rename your controller as products_controller.rb
Then change
class Product::ProductController < ApplicationController
-------------
end
to
class Product::ProductsController < ApplicationController
-------------
end
Even routes needs to be changed.
If you want products controller inside a namespace product
as you mentioned in your question, then you have to declare routes as shown below.
namespace :product do
resources :products
end