简体   繁体   English

如何将产品添加到 Devise 用户 model

[英]How to add products to the Devise user model

I am beginner in Ruby on Rails and am trying to create a Shopper application in rails.我是 Rails 上 Ruby 的初学者,我正在尝试在 Rails 中创建 Shopper 应用程序。 There are 3 models: Batch, Product, User(Devise).有 3 个模型:批次、产品、用户(设计)。 Each batch has many products and each user has many products.每个批次有很多产品,每个用户都有很多产品。 I have created an association(has_many) between User and Product.我在用户和产品之间创建了一个关联(has_many)。 How to add the specific product(Each product has a link to the products path) to the current user logged in if I click the link Buy.如果单击链接购买,如何将特定产品(每个产品都有指向产品路径的链接)添加到当前登录的用户。

My User model:我的用户 model:

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

My Product model:我的产品 model:

class Product < ApplicationRecord
  belongs_to :batch
  belongs_to :user
  validates :name, presence: true

end

Routes file:路由文件:

Rails.application.routes.draw do

  devise_for :users
  root to: 'batches#index'
  resources "batches", only: %i[index show]
  resources "products"
end

Schema file:架构文件:

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

Products controller:产品 controller:

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

Suggest me to specify the redirect_to links if the product is added to the user.如果将产品添加给用户,建议我指定 redirect_to 链接。 I am printing a message.我正在打印一条消息。 Clearly it didn't print it.显然它没有打印出来。 I also referred this: How to associate a Devise User with another existing model?我还提到了这个: 如何将 Devise 用户与另一个现有的 model 关联?

Thanks in advance.提前致谢。

I am a bit confused about the actual problem you are having, but if your question is about links to your newly created Product... Your routes file defines products as:我对您遇到的实际问题有点困惑,但是如果您的问题是关于指向您新创建的产品的链接...您的路线文件将产品定义为:

resources 'products'

This will create a set of routes that have the base form:这将创建一组具有基本形式的路由:

<root>/product/:id

So, the URI namespace for your products is not currently scoped by user, and all you need is a product ID.因此,您的产品的 URI 命名空间当前不受用户范围的限制,您只需要一个产品 ID。 So, if you have want to include a link to a user's set of products, you could do something like:因此,如果您想包含指向用户产品集的链接,您可以执行以下操作:

<% current_user.products.each do |product|%>
  <%= link_to product.name, product_path(product.id) %>
<% end %>

If you really want the paths to products to be scoped by each user, you can do that too.如果您真的希望产品的路径由每个用户限定范围,您也可以这样做。 The routes file allows you to nest resources inside another like this:路由文件允许您将资源嵌套在另一个文件中,如下所示:

resource :users do
  resources :products
end

That will generate routes of the form:这将生成以下形式的路线:

<root>/users/:user_id/products/:id

I suggest checking out the Rails Guide page on routes ( https://guides.rubyonrails.org/routing.html ) to get an idea of the various things you can do to set up routes the way you want them.我建议查看有关路线的 Rails 指南页面 ( https://guides.rubyonrails.org/routing.html ) 以了解您可以做的各种事情来按照您想要的方式设置路线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM