Skip to content
Browse files

Add Multiple adding to car and add comments page in costomers view, C…

…lose gh-20
  • Loading branch information...
1 parent 2848d37 commit f2362ba6eeb7dbe4b288dd9073486e3f1632fd4c @HQYuan711 HQYuan711 committed Aug 10, 2011
View
6 app/controllers/customers_controller.rb
@@ -116,10 +116,8 @@ def remove_collection
end
def show_comments
-
+ @customer = Customer.find(session[:user_id])
+ @comments = Comment.find_comments_by_user(@customer)
end
-
-
-
end
View
2 app/controllers/line_items_controller.rb
@@ -44,7 +44,7 @@ def edit
# POST /line_items.json
def create
@cart = current_cart
- @line_item = @cart.add_product(params[:product_id])
+ @line_item = @cart.add_product(params[:product_id], params["quantity"+params[:product_id]])
@chendishi
Collaborator
chendishi added a note Aug 17, 2011

params里面不要嵌套使用params

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
respond_to do |format|
if @line_item.save
format.html { redirect_to (store_url) }
View
8 app/models/cart.rb
@@ -2,16 +2,16 @@ class Cart < ActiveRecord::Base
has_many :line_items, :dependent => :destroy
belongs_to :customer
- def add_product(product_id)
+ def add_product(product_id, quantity)
current_item = line_items.where(:product_id => product_id).first
if current_item
- current_item.quantity +=1
+ current_item.quantity += quantity.to_f
else
- current_item = LineItem.new(:product_id => product_id)
+ current_item = LineItem.new(:product_id => product_id, :quantity => quantity.to_f)
line_items << current_item
end
product = Product.find(product_id)
- product.heat += 1
+ product.heat += quantity.to_f
product.save
current_item
end
View
12 app/views/customers/show_comments.html.erb
@@ -0,0 +1,12 @@
+<div id="comments">
+ <b>Comments:</b>
+ <span>Total:<%= @comments.length %></span>
+ <% @comments.each do |comment| %>
+ <div class="comment">
+ <span class="titleline">Title:<%= comment.title %> By:<%= @customer.name %></span>
+ <p>Comment:<%= comment.comment %></p>
+ </div>
+ <%= button_to 'delete',{:controller=> 'comments',:action=> 'destroy',:id => comment.id, :product_id => comment.commentable_id} %>
+ <hr />
+ <% end %>
+</div>
View
10 app/views/store/index.html.erb
@@ -17,7 +17,15 @@
<%= periodically_call_remote(:update => { :success => product.id },
:url => { :action => 'get_heat', :product_id => product.id },
:frequency => 30) %>
- <%= button_to t('.add').html_safe, line_items_path(:product_id => product),:remote=>true ,:disabled =>@is_user%>
+ <script type="text/javascript">var changeNum<%= product.id %> = new classChangeNum('<%= 'quantity'+product.id.to_s %>');</script>
+ <%= form_tag(:controller=> 'line_items', :action=> 'create', :product_id => product.id, :remote =>true, :multipart => true, :method => :post) do -%>
+ <input type="button" value="-" onmousedown="changeNum<%= product.id %>.DOWN()" onmouseup="changeNum<%= product.id %>.mouseUpHandle()">
+ <%= number_field_tag 'quantity'+product.id.to_s, 1, :class => "quantity", :onchange => "if(isNaN(this.value)){alert('Quantity can only be a number!');this.value='1';}"%>
@chenmingliang
Collaborator

DRY。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ <input type="button" value="+" onmousedown="changeNum<%= product.id %>.UP()" onmouseup="changeNum<%= product.id %>.mouseUpHandle()">
+ <%= submit_tag t('.add') %>
+ <% end -%>
+
+ <!--<%= button_to t('.add').html_safe, line_items_path(:product_id => product),:remote=>true ,:disabled =>@is_user%>-->
<%= button_to 'collect',{:controller=> 'customers',:action=> 'collect',:product_id => product.id},:disabled =>@is_user%>
</div>
</div>
View
49 public/javascripts/application.js
@@ -16,4 +16,53 @@ window.onload=function(){
this.className="over";
}
}
+}
+
+function classChangeNum(textBoxId)
+{
+ var step = 1;
+ var changeStepTimer = null;
+ var setValueTimer = null;
@chenmingliang
Collaborator

变量名setValueTimer,以set开头,看起来更像是方法名而不是变量名,建议修改。变量名changStepTimer同样有次问题。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ /*改变速度私有方法*/
+ var changeStep = function()
+ {
+ changeStepTimer = setInterval(function(){step += 1}, 1000);//每隔1秒速度加5
+ }
+ /*设置值私有方法*/
+ var setValueIncrease = function()
+ {
+ var textValue = parseInt($(textBoxId).value);
+ if((textValue + step) < 100)
+ $(textBoxId).value = textValue + step;
+ else
+ $(textBoxId).value = 99;
+ setValueTimer = setTimeout(setValueIncrease,200); //每隔200毫秒更数文本框数值一次
+ }
+ var setValueDecrease = function()
+ {
+ var textValue = parseInt($(textBoxId).value);
+ if((textValue - step) > 0)
+ $(textBoxId).value = textValue - step;
+ else
+ $(textBoxId).value = 1;
+ setValueTimer = setTimeout(setValueDecrease,200); //每隔200毫秒更数文本框数值一次
+ }
+ /*按下鼠标处理函数*/
+ this.UP = function()
+ {
+ changeStep();
+ setValueIncrease();
+ }
+ this.DOWN = function()
+ {
+ changeStep();
+ setValueDecrease();
+ }
+ /*松开鼠标处理函数*/
+ this.mouseUpHandle = function()
+ {
+ clearInterval(changeStepTimer);//停止变速和改变文本框的值
+ clearTimeout(setValueTimer);
+ step = 1; //恢复默认速度
+ }
}
View
4 public/stylesheets/store.css
@@ -55,6 +55,10 @@
margin-bottom: 0.5em;
}
+#storeindex .price_line .quantity{
+ width: 2em;
+}
+
#storeindex .price, #storeindex .heat {
color: #4444AA;
font-weight: bold;
View
4 test/functional/line_items_controller_test.rb
@@ -18,7 +18,7 @@ class LineItemsControllerTest < ActionController::TestCase
test "should create line_item" do
assert_difference('LineItem.count') do
- post :create, :product_id => products(:ruby).id
+ post :create, :product_id => products(:ruby).id.to_s, ("quantity"+products(:ruby).id.to_s) => 1
end
assert_redirected_to store_path
end
@@ -29,7 +29,7 @@ class LineItemsControllerTest < ActionController::TestCase
product.category_ids = [categories(:one).id]
product.save
- post :create, :product_id => products(:ruby).id#:line_item => @line_item.attributes
+ post :create, :product_id => products(:ruby).id.to_s, ("quantity"+products(:ruby).id.to_s) => 1
end
end

0 comments on commit f2362ba

Please sign in to comment.
Something went wrong with that request. Please try again.