【问题标题】:How to fixed the value in input field and also set default zero如何固定输入字段中的值并设置默认零
【发布时间】:2022-09-28 19:02:34
【问题描述】:

现在我做一个可以将产品添加到出口的表格。这个表格是在出口产品页面上创建的

所以当我点击产品信息页面有一个按钮(将产品添加到出口)然后转到位于出口产品页面的表格。现在我需要设置这些东西

1.我已经创建了表格,但是在产品名称字段中我需要显示我选择固定且不能更改的产品名称。(例如,单击产品鱼然后单击按钮,那么产品名称应该是鱼。 )

2. sell_price 和 last_cost 需要在输入字段中显示产品价格和成本,但这可以更改(例如,在产品页面价格为 2 美元,成本为 1 美元,那么这里的输入字段将是 sell_price $2 和 last_cost 1美元。)

3.数量必须默认为零。

我已经尝试过,但它不会工作。

出口产品控制器

 class OutletProductsController < ApplicationController
  def new
   @outlet_product = OutletProduct.new
   @product = Product.all
   @outlet = Outlet.all
   @category = Category.all
  end

  def index
  end

  def show
  end

  def create
   @outlet_product = OutletProduct.new(outlet_product_params)
   @category_id = Category.all
   @outlet_id = Outlet.all
   @product_id = Product.all
    if @outlet_product.save
    flash[:success] = \"Succesful create!\"
    redirect_to @outlet_product
  else
    render \'new\'
  end
 end

 def edit
 end

 def outlet_product_params
 params.require(:outlet_product).permit(:product_id, :outlet_id, :quantity,
                                       :selling_price ,:last_cost)
  end
end

新的.html.erb

<h1>Add product to outlet</h1>
 <div class=\"row\">
  <div class=\"col-md-6 col-md-offset-3\">
   <%= form_with(model: @outlet_product, local: true) do |f| %>
   <%= render \'shared/error_messages\', object: f.object %>

  <%= f.label :product_name %>
  <%= f.text_field :@product.name ,class: \"form-select\" %>

  <%= f.label :quantity %>
  <%= f.number_field :quantity%>

  <%= f.label :selling_price %>
  <%= f.number_field :selling_price, @product.price , class: \'form-control\' %>

  <%= f.label :last_cost %>
  <%= f.number_field :last_cost,@product.cost, class: \'form-control\' %>

  <%= f.label :outlet_id %>
  <%= f.select(:outlet_id, Outlet.all.collect { |l| [ l.name, l.id] }, {class: \"form-select\"}) %>

  <%= f.submit \"Submit\", class: \"btn btn-primary\" %>
  <% end %>
 </div>
</div>

模式中的 OutletProduct 迁移

create_table \"outlet_products\", force: :cascade do |t|
  t.integer \"outlet_id\"
  t.integer \"product_id\"
  t.datetime \"created_at\", null: false
  t.datetime \"updated_at\", null: false
  t.decimal \"selling_price\"
  t.decimal \"last_cost\"
  t.decimal \"quantity\"
end

架构中的产品迁移

create_table \"products\", force: :cascade do |t|
 t.string \"name\"
 t.integer \"quantity\"
 t.integer \"price\"
 t.integer \"category_id\"
 t.datetime \"created_at\", null: false
 t.datetime \"updated_at\", null: false
 t.decimal \"cost\"
end

    标签: ruby-on-rails ruby


    【解决方案1】:

    嗨,您可以在视图中定义所有默认值,如下所示

    <%= f.label :selling_price %>
    <%= f.number_field :selling_price, 0 , class: 'form-control' %>
    

    或者如果您想将其默认保留在数据库中,那么您可以在迁移文件中进行如下更改:

    create_table "products", force: :cascade do |t|
     t.string "name", default: 'fish'
     t.integer "quantity", default: 0
     t.integer "price"
     t.integer "category_id"
     t.datetime "created_at", null: false
     t.datetime "updated_at", null: false
     t.decimal "cost"
    end
    

    我希望这是你想要达到的目标。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-08
      • 1970-01-01
      相关资源
      最近更新 更多