首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Rails:将产品动态添加到订单

Rails:将产品动态添加到订单
EN

Stack Overflow用户
提问于 2013-05-13 00:26:31
回答 1查看 177关注 0票数 0

我希望能够动态地添加表单元素到我的订单页面,这样用户可以点击一个按钮,并得到另一个元素来选择产品。然后是如何使用Rails来处理这个问题。以下是我当前的文件:

订单模型:

代码语言:javascript
运行
复制
class Order < ActiveRecord::Base
  attr_accessible :client_id, :order_total, :delivery_date
  has_many :orderedproducts
  has_many :products, through: :orderedproducts
  has_one :client

  before_save :generate_total

  def generate_total
    self.order_total = self.products.map(&:product_price).sum
  end
end

订单控制器:

代码语言:javascript
运行
复制
class OrdersController < ApplicationController
  def view
    @orders = Order.all
  end

  def new
    @order = Order.new
  end
end

新建订单视图:

代码语言:javascript
运行
复制
<% if current_user %>
    <div id="dashboard">
        <div id="logo"></div>
        <table id="go_back_link_container">
            <tr>
                <td>
                    <div class="go_back_link">
                        <%= link_to "<- Go Back", root_url %>
                    </div>
                </td>
                <td>
                    <div id="user_display">
                        Logged in as <%= current_user.email %>.
                        <%= link_to "Log out", log_out_path %>
                    </div>
                </td>
            </tr>
        </table>
        <%= form_for @order do |f| %>
          <% if @order.errors.any? %>
            <div class="error_messages">
                <% for message in @order.errors.full_messages %>
                    * <%= message %> <br>
                <% end %>
            </div>
          <% end %>
          <p>
            <%= f.label 'Select The Client' %><br />
            <%= select :client, :client, Client.all().collect { |c| [ (c.firstname + " " + c.surname), c.id ] } %>
          </p>
           <p>
            <%= f.label 'Select The Client' %><br />
            <%= f.fields_for :products do |builder| %>
                <%= render '', f: builder %>
            <% end %>
          </p>
          <p class="button"><%= f.submit %></p>
        <% end %>
        <% flash.each do |name, msg| %>
            <%= content_tag :div, "* " + msg, :id => "flash_#{name}" %><br />
        <% end %>
        <div id="copyright-notice"><div id="copyright_border">Copyright © Conner McCabe, all rights reserved.</div></div>
    </div>
<% else %>
    <script type="text/javascript">
        window.location="<%= root_url %>"
    </script>
<% end %>

订购的产品型号:

代码语言:javascript
运行
复制
class Orderedproduct < ActiveRecord::Base
  attr_accessible :order_id, :product_id, :quantity_ordered
  belongs_to :order
  belongs_to :product
end

产品型号:

代码语言:javascript
运行
复制
class Product < ActiveRecord::Base
    #This line makes these elements accessible outside of the class.
    attr_accessible :product_name, :product_price, :product_quantity, :product_supplier

    has_many :orderedproducts
    has_many :orders, through: :orderedproducts

    #These attributes ensure that the data entered for each element is valid and present.
    validates_presence_of :product_name
    validates_presence_of :product_price
    validates_numericality_of :product_price
    validates_presence_of :product_quantity
    validates_numericality_of :product_quantity
    validates_presence_of :product_supplier

end

订单和产品之间的关系是通过有序产品模型来实现的。所以我需要能够添加一个包含所有产品的下拉列表和数量文本字段的字段,以便我可以输入他们希望添加到订单的数量。

非常感谢您的帮助,谢谢。

EN

回答 1

Stack Overflow用户

发布于 2013-05-13 01:37:25

你可能想看看这个- Building Complex Forms in Rails

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16509516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档