新手问题 看不懂这几行代码

jiwoorico · August 15, 2013 · Last by jiwoorico replied at August 16, 2013 · 2112 hits

新手最近在看 agile web development with rails 电子版,有几行代码看不明白,求教:

  1. button_to
<%= button_to 'Add to Cart', line_items_path(product_id: product) %>

看不懂为什么这个 line_items_path(product_id: product) 调用了 line_items controller 的 create 方法?哪里规定的?

  1. 测试脚本
Download rails32/depot_g/test/functional/line_items_controller_test.rb
test "should create line_item" do
assert_difference('LineItem.count') do
➤ post :create, product_id: products(:ruby).id
end
➤ assert_redirected_to cart_path(assigns(:line_item).cart)
end

这里 (:ruby) 什么用途? 谢谢啊

1 Floor has deleted

line_items_path,根据 routes.rb 文件定义的路由,执行对应 controller 里面的 action。这个你可以运行下rake routes来了解你的路由映射关系

嗯,看到了。thanks, Ryan. 那第二个问题怎么解释啊?

@jiwoorico 看你的 fixtures 文件,里面应该有

#5 楼 @leomayleomay 嗯,看到了。谢谢

You need to Sign in before reply, if you don't have an account, please Sign up first.