我有一个rails表格,用户可以选择每小时或每天购买一个产品。我正在使用购物车跟踪购买的每件商品。提交表单时,将向购物车传递订单项对象。我需要根据用户是按小时还是每天选择购买来更改商品的价格并更改为成本计算。我不是应该如何测试参数,如果我应该在项目的模型或控制器中执行此操作。
我的代码如下。谢谢你的帮助!
填写的表单
<%= form_for LineItem.new do |f| %>
<p><%= render :partial => "price" %> /
<%= f.select :day_or_hour, [['day', 'day'], ['hour', 'hour']], :id => 'day_or_hour' %>
<div class="gearside_date_main">
<h3 style="font-family: 'RockSaltRegular', 'JosefinSansStdLight', Helvetica, Arial, sans-serif;color: #71a41e; font-size: 16px; margin: 15px 0 5px 15px;">Rental Date</h3>
<%= f.text_field :rentstart, id: 'rentstart', :value => "Pickup" %>
<%= f.text_field :rentend, id: 'rentend', :value => "Drop Off" %>
<%= image_tag('calendar.png', :style => "float:left; padding-top: 8px") %>
<%= f.hidden_field :gear_id, :value => @gear.id %>
</br></br>
<div class="gear_time_schedule hourlyshowhide">
<span class="gear_time_schedule_container">
<%= f.label :starthour, 'Pick Up Time', class: 'labeltext' %>
<%= f.text_field :starthour, id: 'starthour', :value => '' %>
</span>
<span class="gear_time_schedule_container">
<%= f.label :endhour, 'Drop Off Time', class: 'labeltext' %>
<%= f.text_field :endhour, id: 'endhour', :value => '' %>
</span>
</div>
<%= f.submit "", id: 'rent_it' %>
<% end %>
齿轮模型
class Gear < ActiveRecord::Base
belongs_to :user
has_many :line_items
def hourly_price
price/24
end
end
订单项模型
class LineItem < ActiveRecord::Base
belongs_to :cart
belongs_to :gear
def total_price
gear.price * quantity
end
def set_rentprice price
self.rentprice = price
end
end
订单项数据库中的参数
`id` int(11) NOT NULL AUTO_INCREMENT,
`gear_id` int(11) DEFAULT NULL,
`cart_id` int(11) DEFAULT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
`quantity` int(11) DEFAULT '1',
`rentstart` date DEFAULT NULL,
`rentend` date DEFAULT NULL,
`rentprice` decimal(10,0) DEFAULT NULL,
`starthour` varchar(255) DEFAULT NULL,
`endhour` varchar(255) DEFAULT NULL,
`day_or_hour` varchar(255) DEFAULT NULL,
`order_id` int(11) DEFAULT NULL,
在LineItem控件中创建操作
def create
@cart = current_cart
case params[:day_or_hour]
when 'day'
price = Gear.weekly_price
when 'hour'
price = Gear.hourly_price # these methods have to be converted to class methods
end
gear = Gear.find(params[:line_item][:gear_id])
lineitem = LineItem.new(params[:line_item])
lineitem.set_rentprice price
@line_item = @cart.add_gear(lineitem, gear.id)
respond_to do |format|
if @line_item.save
format.html { redirect_to @line_item.cart }
format.json { render json: @line_item, status: :created, location: @line_item }
else
format.html { render action: "new" }
format.json { render json: @line_item.errors, status: :unprocessable_entity }
end
end
end
购物车型号
class Cart < ActiveRecord::Base
has_many :line_items, dependent: :destroy
def add_gear(lineitem, gear_id)
current_item = line_items.find_by_gear_id(gear_id)
if current_item
current_item.quantity += 1
else
current_item = line_items.build(lineitem)
end
current_item
end
def total_price
line_items.to_a.sum { |item| item.total_price }
end
端
答案 0 :(得分:0)
请允许我建议一个重新考虑因素。
这看起来像许多协会的好候选人。 安装nested_form gem。有关嵌套表单的更多信息,请查看railscasts
class Cart < ActiveRecord::Base
has_many :line_items
has_many :gears, through: line_items
attr_accessible :line_items_attributes
accepts_nested_attributes_for :line_items
def total_price
gears.sum(:price)
end
end
class Gear < ActiveRecord::Base
has_many :line_items
has_many :carts, through: line_items
end
class LineItem < ActiveRecord::Base
belongs_to :cart
belongs_to :gear
attr_accessible :cart_id, :gear_id
end
现在,在您的购物车控制器中
def new
@cart = Cart.new
end
我使用simple_form来获得更清晰的观点。你应该考虑使用它。
Nested_form将负责添加&amp;通过jquery删除项目:)
<%= simple_nested_form_for @cart do |f| %>
<%= f.simple_fields_for :line_items do |item| %>
<%= item.input :rentstart %>
<%= item.input :rentend %>
#Select your gear like this
<%= item.association :gear, as: :collection, label_method: :gear.nameorwhatever, value_method: :gear.id %>
#Remove items using this link
<%= item.link_to_remove "Remove this task" %>
<% end %>
#Add new item
<p><%= f.link_to_add "Add an item", :tasks %></p>
<% end %>
您的购物车控制器的创建操作将是标准的。 accepts_nested_attributes会神奇地更新你的lineItem。