【问题标题】:Turning one class instance into the instance of another class将一个类实例变成另一个类的实例
【发布时间】:2015-07-15 13:35:14
【问题描述】:

我正在建立一个艺术家社区,供同行评审提交的作品。我希望管理员将具有良好评论的作品变成具有原始作品模型中不存在的价格、颜色变体和其他属性的产品,并删除评论等属性。一旦管理员将其转换为产品,片段实例就会被销毁。我希望管理员通过表单来完成此操作。

ActiveRecord::Schema.define(version: 20150715145051) do

  create_table "prints", force: :cascade do |t|
    t.string   "name"
    t.text     "description"
    t.integer  "pledge"
    t.integer  "rating"
    t.string   "category"
    t.datetime "created_at",         null: false
    t.datetime "updated_at",         null: false
    t.integer  "user_id"
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
    t.boolean  "in_production"
  end

  create_table "products", force: :cascade do |t|
    t.string   "name"
    t.string   "creator"
    t.text     "description"
    t.datetime "created_at",         null: false
    t.datetime "updated_at",         null: false
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
  end

  create_table "reviews", force: :cascade do |t|
    t.integer  "rating"
    t.text     "comment"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "subproducts", force: :cascade do |t|
    t.integer  "price"
    t.integer  "quantity"
    t.string   "size"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  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.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.string   "avatar_file_name"
    t.string   "avatar_content_type"
    t.integer  "avatar_file_size"
    t.datetime "avatar_updated_at"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

  end

我想将印刷品转变为产品。我可以举例说明如何通过表格完成此操作吗?我不需要全部代码,只需要这个想法的要点。谢谢。

【问题讨论】:

  • 你能问一个具体的问题吗?
  • 就目前而言,您要求人们为您编写一个完整的应用程序! :)

标签: ruby-on-rails ruby


【解决方案1】:

这是一个不受欢迎的问题,但一段时间后我发现了如何做我想做的事。我将为任何想做我想做的事情的人发布解决方案。 所以我想在产品页面中制作一个打印实例。我在创建新产品实例的打印控制器中设置了一个控制方法,使用@print 的值来设置产品属性值:

def turn_into_product
  @product = Product.new(image: @print.image, name: @print.name, \
      description: @print.description, user_id: @print.user_id, print_id: @print.id, \
   creator: @print.user.username)
respond_to do |format|
  if @product.save
    format.html { redirect_to @product, notice: 'Product was successfully created.' }
    format.json { render :show, status: :created, location: @product }
  else
    format.html { render :new }
    format.json { render json: @product.errors, status: :unprocessable_entity }
  end
end

然后我在路由文件中建立了这个方法的连接:

resources :prints do
  member do
    put 'flag', 'turn_into_product'
  end
end

最后,我在打印显示页面上使用了一个 'put' call link_to 按钮:

<%= link_to "Turn into Product", turn_into_product_print_path(@print), method: :put, class: "btn btn-success" %>

瞧,具有所有属性的新产品实例,包括包含的图像。在提出问题时,我对 link_to 中的 html 方法调用或如何使用 put 方法的路由没有扎实的掌握。我也不知道您可以在外部控制器中创建另一个类的实例。现在我这样做了,可能性似乎成倍增加。

【讨论】:

    猜你喜欢
    • 2015-06-09
    • 2023-03-28
    • 2020-04-28
    • 2013-01-14
    • 1970-01-01
    • 2017-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多