如何将产品添加到Devise用户模型中?

问题描述 投票:0回答:1

我是Ruby on Rails的初学者,正试图在Rails中创建一个Shopper应用程序。有3个模型:Batch,Product,User(Devise),每个批次有很多产品,每个用户有很多产品。每个批次有很多产品,每个用户有很多产品。我在用户和产品之间创建了一个关联(has_many)。如何将特定的产品(每个产品都有一个链接到产品路径)添加到当前登录的用户,如果我点击链接购买。

我的用户模型。

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

Schema文件:我的用户模型:我的产品模型:路由文件:模式文件。

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链接。我在打印一条信息。很明显没有打印出来。我还参考了这个。如何将一个Devise用户与另一个现有模型关联起来?

先谢谢你。

ruby-on-rails ruby devise associations ruby-on-rails-6
1个回答
0
投票

我对你所遇到的实际问题有点困惑,但如果你的问题是关于新创建的产品的链接...。您的路由文件将产品定义为:

resources 'products'

这将会创建一组具有基本形式的路由。

<root>/product/:id

所以,你的产品的URI命名空间目前还没有被用户定义, 你需要的只是一个产品ID。所以,如果你有想要包含一个用户的产品集的链接,你可以做这样的事情。

<% 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.orgrouting.html。)来了解你可以做的各种事情来设置你想要的路线。

© www.soinside.com 2019 - 2024. All rights reserved.