I just picked up Agile Web Development with Rails 3rd Ed., and I'm going thru the Depot Application chapters, and I have a question about Product/Item options-
我刚刚学习了敏捷Web开发,Rails是第3版,我将通过仓库应用程序章节,我有一个关于产品/项目选项的问题
If I wanted to modify the product catalog and store so that products could have options (size, color, whatever), where/how would I do that?
如果我想修改产品目录和存储,使产品可以有选项(大小、颜色等等),我应该在哪里/如何做呢?
Let's say I'm selling t-shirts, and they come in different sizes. I don't feel like that's something that really needs a model created to handle sizes, so I thought I could just add it as a select box in the html in the store's view.
假设我在卖t恤,它们有不同的尺寸。我不觉得这真的需要一个模型来处理大小,所以我想我可以把它作为一个选择框添加到商店视图的html中。
But, each Add to Cart button is wrapped by a form tag that is automatically generated by button_to, and doesn't seem to give me the ability to pass additional parameters to my cart. How can I get the size of the item added into the POST to add_to_cart?
但是,每个Add to Cart按钮都是由一个由button_to自动生成的表单标记包装的,而且似乎不能给我传递额外参数到我的购物车的能力。如何获得添加到add_to_cart的邮件的大小?
And perhaps more importantly- what is the most Railsy way to do so?
也许更重要的是——最可笑的做法是什么?
Thanks in advance for any help! --Mark
感谢您的帮助!——马克。
The helper in my view:
在我看来帮助者:
<%= button_to "Add to Cart" , :action => :add_to_cart, :id => product %>
The form that it generates:
它产生的形式:
<form method="post" action="/store/add_to_cart/3" class="button-to">
4 个解决方案
#1
4
Ok, it's 2 days later, and I figured it out. This is what I had to do-
好的,两天后,我算出来了。这是我必须做的。
1, in my store view:
<% form_for @product, :url => {:action => "add_to_cart", :id => @product} do |f| %>
<select name="productsize" id="productsize">
<option value="L">L</option>
<option value="XL">XL</option>
</select>
<%= f.submit 'Add to Cart' %>
<% end %>
2, added to my store controller:
productsize = params[:productsize]
@cart.add_product(product, productsize)
Had to get productsize from params, and then pass it with the rest of the product model to the cart model's add_product action.
必须从params获取产品大小,然后将产品模型的其余部分传递给cart模型的add_product操作。
3, adjusted the cart model to accept the argument, and:
@items << CartItem.new(product, productsize)
Passed it along with the rest of the product model to create a new Cart Item and add it to items.
将它与产品模型的其余部分一起传递,以创建新的Cart项并将其添加到items中。
4, added to the cart_item model:
attr_reader :product, :quantity, :productsize
def initialize(product, productsize)
@product = product
@productsize = productsize
to read in productsize and initialize Cart Item.
读取productsize并初始化Cart项。
5, added to my add_to_cart view:
Size: <%=h item.productsize %>
To display it for the user.
以显示给用户。
That's it. If there is an easier or DRYer way to go about it, I'm all ears (eyes?).
就是这样。如果有一种更简单或更干的方式,我会全神贯注(眼睛?)
#2
1
I'm not sure why you wouldn't store size, unless what you mean is that you'd store size as part of cart_item rather than product, which would be fine. In that case you'd do something like this:
我不确定为什么不存储大小,除非您的意思是您将大小存储为cart_item而不是product的一部分,这样很好。在这种情况下,你会这样做:
<% form_for(@cart_item) do |f| %>
<%= f.select :size, ['S', 'M', 'L', 'XL', 'XXL'] %>
<%= f.hidden_field :product_id, :value => @product.id %>
# other properties...
<%= f.submit 'Add to Cart' %>
<% end %>
#3
0
I'd drop the button_to
helper and use a proper form, submitting the product properties to the add_to_cart
action.
我将删除button_to helper,并使用适当的表单,将产品属性提交给add_to_cart操作。
<% form_for(@product) do |f| %>
<%= f.select :size, ['S', 'M', 'L', 'XL', 'XXL'] %>
# other properties...
<%= f.submit 'Add to Cart' %>
<% end %>
#4
0
You will need to add attributes to your model. For that, you will need to create a migration to update your database table. I only have the 2nd edition of the book, but there's a section called "Iteration A2: Add a missing column" that describes how to do this. I assume a similar section would be in the 3rd edition.
您将需要向模型添加属性。为此,需要创建迁移来更新数据库表。我只有这本书的第二版,但是有一个章节叫做“迭代A2:添加一个缺失的列”,描述了如何做到这一点。我想在第三版中也会有类似的章节。
After doing that, you can follow Can Berk Güder's suggestion and replace the button by a form.
这样做之后,您可以按照can Berk Guder的建议,用表单替换按钮。
#1
4
Ok, it's 2 days later, and I figured it out. This is what I had to do-
好的,两天后,我算出来了。这是我必须做的。
1, in my store view:
<% form_for @product, :url => {:action => "add_to_cart", :id => @product} do |f| %>
<select name="productsize" id="productsize">
<option value="L">L</option>
<option value="XL">XL</option>
</select>
<%= f.submit 'Add to Cart' %>
<% end %>
2, added to my store controller:
productsize = params[:productsize]
@cart.add_product(product, productsize)
Had to get productsize from params, and then pass it with the rest of the product model to the cart model's add_product action.
必须从params获取产品大小,然后将产品模型的其余部分传递给cart模型的add_product操作。
3, adjusted the cart model to accept the argument, and:
@items << CartItem.new(product, productsize)
Passed it along with the rest of the product model to create a new Cart Item and add it to items.
将它与产品模型的其余部分一起传递,以创建新的Cart项并将其添加到items中。
4, added to the cart_item model:
attr_reader :product, :quantity, :productsize
def initialize(product, productsize)
@product = product
@productsize = productsize
to read in productsize and initialize Cart Item.
读取productsize并初始化Cart项。
5, added to my add_to_cart view:
Size: <%=h item.productsize %>
To display it for the user.
以显示给用户。
That's it. If there is an easier or DRYer way to go about it, I'm all ears (eyes?).
就是这样。如果有一种更简单或更干的方式,我会全神贯注(眼睛?)
#2
1
I'm not sure why you wouldn't store size, unless what you mean is that you'd store size as part of cart_item rather than product, which would be fine. In that case you'd do something like this:
我不确定为什么不存储大小,除非您的意思是您将大小存储为cart_item而不是product的一部分,这样很好。在这种情况下,你会这样做:
<% form_for(@cart_item) do |f| %>
<%= f.select :size, ['S', 'M', 'L', 'XL', 'XXL'] %>
<%= f.hidden_field :product_id, :value => @product.id %>
# other properties...
<%= f.submit 'Add to Cart' %>
<% end %>
#3
0
I'd drop the button_to
helper and use a proper form, submitting the product properties to the add_to_cart
action.
我将删除button_to helper,并使用适当的表单,将产品属性提交给add_to_cart操作。
<% form_for(@product) do |f| %>
<%= f.select :size, ['S', 'M', 'L', 'XL', 'XXL'] %>
# other properties...
<%= f.submit 'Add to Cart' %>
<% end %>
#4
0
You will need to add attributes to your model. For that, you will need to create a migration to update your database table. I only have the 2nd edition of the book, but there's a section called "Iteration A2: Add a missing column" that describes how to do this. I assume a similar section would be in the 3rd edition.
您将需要向模型添加属性。为此,需要创建迁移来更新数据库表。我只有这本书的第二版,但是有一个章节叫做“迭代A2:添加一个缺失的列”,描述了如何做到这一点。我想在第三版中也会有类似的章节。
After doing that, you can follow Can Berk Güder's suggestion and replace the button by a form.
这样做之后,您可以按照can Berk Guder的建议,用表单替换按钮。