如何将产品添加到 Devise 用户模型
How to add products to the Devise user model
我是 Rails Ruby 的新手,我正在尝试在 rails 中创建一个 Shopper 应用程序。有 3 个模型:批次、产品、用户(设计)。每个批次有很多产品,每个用户有很多产品。我在用户和产品之间创建了一个关联(has_many)。如果我单击 link 购买,如何将特定产品(每个产品在产品路径中都有一个 link)添加到当前登录的用户。
我的用户模型:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
has_many :products, :dependent => :destroy
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
end
我的产品型号:
class Product < ApplicationRecord
belongs_to :batch
belongs_to :user
validates :name, presence: true
end
路线文件:
Rails.application.routes.draw do
devise_for :users
root to: 'batches#index'
resources "batches", only: %i[index show]
resources "products"
end
架构文件:
ActiveRecord::Schema.define(version: 2020_06_16_134907) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "batches", force: :cascade do |t|
t.string "name"
t.integer "code"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "products", force: :cascade do |t|
t.string "name"
t.bigint "batch_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.bigint "user_id"
t.index ["batch_id"], name: "index_products_on_batch_id"
t.index ["user_id"], name: "index_products_on_user_id"
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
add_foreign_key "products", "batches"
end
产品负责人:
class ProductsController < ApplicationController
def show
@product = Product.find(params[:id])
end
def index
@products = Product.order(:name)
end
def new
@product = Product.new
end
def create
@product = current_user.products.build(params[:product])
if @product.save
puts 'yes'
else
puts 'no'
end
end
private
def prod_params
params.require(:product).permit(:name, :user_id)
end
end
建议我指定 redirect_to link 如果产品被添加到用户。我正在打印一条消息。显然它没有打印出来。
我还提到了这个:How to associate a Devise User with another existing model?
提前致谢。
我对你遇到的实际问题有点困惑,但如果你的问题是关于 links 到你新创建的产品......你的路线文件将产品定义为:
resources 'products'
这将创建一组具有基本形式的路线:
<root>/product/:id
因此,您的产品的 URI 命名空间目前不受用户限制,您只需要一个产品 ID。因此,如果您想要将 link 添加到用户的产品集中,您可以执行以下操作:
<% current_user.products.each do |product|%>
<%= link_to product.name, product_path(product.id) %>
<% end %>
如果您真的希望每个用户都能确定产品路径的范围,您也可以这样做。路由文件允许你像这样在另一个内部嵌套资源:
resource :users do
resources :products
end
这将生成以下形式的路由:
<root>/users/:user_id/products/:id
我建议查看 Rails 路线指南页面 (https://guides.rubyonrails.org/routing.html),以了解您可以按照自己的方式设置路线的各种操作。
我是 Rails Ruby 的新手,我正在尝试在 rails 中创建一个 Shopper 应用程序。有 3 个模型:批次、产品、用户(设计)。每个批次有很多产品,每个用户有很多产品。我在用户和产品之间创建了一个关联(has_many)。如果我单击 link 购买,如何将特定产品(每个产品在产品路径中都有一个 link)添加到当前登录的用户。
我的用户模型:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
has_many :products, :dependent => :destroy
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
end
我的产品型号:
class Product < ApplicationRecord
belongs_to :batch
belongs_to :user
validates :name, presence: true
end
路线文件:
Rails.application.routes.draw do
devise_for :users
root to: 'batches#index'
resources "batches", only: %i[index show]
resources "products"
end
架构文件:
ActiveRecord::Schema.define(version: 2020_06_16_134907) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "batches", force: :cascade do |t|
t.string "name"
t.integer "code"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "products", force: :cascade do |t|
t.string "name"
t.bigint "batch_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.bigint "user_id"
t.index ["batch_id"], name: "index_products_on_batch_id"
t.index ["user_id"], name: "index_products_on_user_id"
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
add_foreign_key "products", "batches"
end
产品负责人:
class ProductsController < ApplicationController
def show
@product = Product.find(params[:id])
end
def index
@products = Product.order(:name)
end
def new
@product = Product.new
end
def create
@product = current_user.products.build(params[:product])
if @product.save
puts 'yes'
else
puts 'no'
end
end
private
def prod_params
params.require(:product).permit(:name, :user_id)
end
end
建议我指定 redirect_to link 如果产品被添加到用户。我正在打印一条消息。显然它没有打印出来。 我还提到了这个:How to associate a Devise User with another existing model?
提前致谢。
我对你遇到的实际问题有点困惑,但如果你的问题是关于 links 到你新创建的产品......你的路线文件将产品定义为:
resources 'products'
这将创建一组具有基本形式的路线:
<root>/product/:id
因此,您的产品的 URI 命名空间目前不受用户限制,您只需要一个产品 ID。因此,如果您想要将 link 添加到用户的产品集中,您可以执行以下操作:
<% current_user.products.each do |product|%>
<%= link_to product.name, product_path(product.id) %>
<% end %>
如果您真的希望每个用户都能确定产品路径的范围,您也可以这样做。路由文件允许你像这样在另一个内部嵌套资源:
resource :users do
resources :products
end
这将生成以下形式的路由:
<root>/users/:user_id/products/:id
我建议查看 Rails 路线指南页面 (https://guides.rubyonrails.org/routing.html),以了解您可以按照自己的方式设置路线的各种操作。