面试时遇到的一个问题,在产品列表前加个上check_box,选择一个或几个产品,提交后到checkout页面,输入客户信息,如email和address等信息后,提交保存。
开始的思路:
把各个product的id传入check_box,提交后从params中获取选中的id值,再从数据库中,根据id值在checkout页面中列出相应的product的信息。
首先:在check_box中光传入id值是不够的,还要给这个check_box设个名字
接着:提交是用一个link_to,link_to也可以接受参数,但怎么把多个id作为参数给这个link_to
因此在处理的action中一直无法得到选中的产品的id值。
后改为:
check_box的name="product[]",value="<%= product.id %>"
然后放在一个form_tag中,而不是通过link_to,这样一提交,选中的id就能在params中。
但在action中如果这样查找:
Product.find(:all, :conditions => ["id = ?", params[:product]] )
就会得到这样一个错误:
Operand should contain 1 column(s): SELECT * FROM `products` WHERE (id = '1','3')
看来已经得到了所选中的产品的ID,但怎么取出来呢?用了个直接办法,既然已经得到了ID,就一个个迭代出来并加到一个数组里:
@products = []
params[:product].each do |p|
@products << Product.find_by_id(p)
end
这样,在下一个页面就可以根据@products迭代出所选中的product了。
这里的checkbox没有用rails提供的标签,可能rails的标签更方便,下次可以试试。
用sacffold创建了product:
ruby script/generate scaffold product name:string price:decimal desc:string owner:string
如果要用form_tag:
<% form_tag checkout_products_path, :method => :put do %>
还要在routes.rb配置路径:
map.resources :products, :collection => {:checkout => :put}
在index.html.erb页面中将所有产品列出:
<h1>Listing products</h1>
<%= link_to '添加新产品', new_product_path %><br>
<%= flash[:notice] %>
<% form_tag checkout_products_path, :method => :put do %>
<table>
<tr>
<th colspan="2">Name</th>
<th>Price</th>
<th>Desc</th>
<th>Owner</th>
</tr>
<% for product in @products %>
<tr>
<td>
<input name="id" value="<%= product.id %>" type="checkbox" />
<%= check_box_tag "product[]", product.id,false %>
</td>
<td><%=h product.name %></td>
<td><%=h product.price %></td>
<td><%=h product.desc %></td>
<td><%=h product.owner %></td>
<td><%= link_to 'Show', product %></td>
<td><%= link_to 'Edit', :controller => :products, :action => :edit, :id => product %></td>
<td><%= link_to 'Destroy', product, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<%= submit_tag "生成订单" %>
<% end %>
其实还可以用form_for:
<% form_for :product, :url => {:action => :checkout} do |f| %>
这样就可以不用配置routes,只要有:
map.resources :products
分享到:
相关推荐
rails_apps_composer, 一个 gem,为 Rails 启动应用程序创建 Rails 应用程序模板 Rails 应用编辑器 Rails 应用程序编辑器 gem 安装一个 命令行 工具来从"食谱"的Collection 组装 Rails 应用程序。"你可以使用 rails_...
InspiniaAdmin 2.6.1 Rails_Full_Version
inspinia admin 2.5 中的 Rails_Full_Version
agile_web_development_with_rails_3rd_edition.9994652073.pdf
rails_semantic_logger, Rails 语义记录器用语义记录器替换 Rails 缺省记录器 Rails 语义记录器 语义记录器用语义记录器替代 Rails 缺省记录器。http://github.com/rocketjob/rails_semantic_logger文档有关完整文档...
介绍插件,用于对记录进行排序(使用 gem)安装要启用rails_admin_acts_as_list,请将以下内容添加到您的Gemfile : gem 'rails_admin_acts_as_list'gem 'rails_admin' 重要提示: rails_admin_acts_as_list之前必须...
rails_email_preview, 在 Rails 中,预览和编辑应用程序邮件程序模板 Rails 电子邮件预览 使用这里 Rails 引擎在浏览器中预览电子邮件。 兼容 Rails 4.2 。电子邮件审阅: 所有电子邮件预览的列表: 代表有两个主题...
RestFul_Rails_Dev_v_0.1 RestFul_Rails_Dev_v_0.1
ruby_on_rails_3_入门教程(中文)
rails_best_practices ...默认情况下,rails_best_practices将解析vendor , spec , test和features目录中的代码。 排除目录 要排除目录,只需使用-e或--exclude调用: rails_best_practices -e "db/migrate
Rails_Recipes_with_Source_Code
Rails_3_Cheat_Sheets.pdf
InspiniaAdmin 2.5 Rails_Seed_Project
InspiniaAdmin 2.5 Rails_full_version
rails_plugins_presentation
inspinia admin 2.5 中的 Rails_Seed_Project
复杂Rails系统架构优化
INSPINIA_Rails_Full_Version-v2.6.2.1
应用Rails进行REST 开发 ,翻译自《RESTful Rails Development》