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:
问题是我想按照用户ID在产品中插入数据但是它说:
uninitialized constant ProductController
未初始化的常量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:
这是我的产品控制器,即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:
这是我的new.html.erb代码:
<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.
我怎样才能将所有零件都制成一个?我的目标是使用新页面添加产品名称并重定向到产品列表页面。
2 个解决方案
#1
0
Rename your controller as products_controller.rb
将控制器重命名为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
#2
0
Rename your controller as products_controller.rb
将控制器重命名为products_controller.rb
Then change
class Product::ProductController < ApplicationController
-------------
end
to
class Product::ProductsController < ApplicationController
-------------
end
and the routes file should be changed as this
并且路由文件应该更改为此
namespace :product do
resources :products
end
instead of resources :product
而不是资源:产品
#1
0
Rename your controller as products_controller.rb
将控制器重命名为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
#2
0
Rename your controller as products_controller.rb
将控制器重命名为products_controller.rb
Then change
class Product::ProductController < ApplicationController
-------------
end
to
class Product::ProductsController < ApplicationController
-------------
end
and the routes file should be changed as this
并且路由文件应该更改为此
namespace :product do
resources :products
end
instead of resources :product
而不是资源:产品