`
cinic2003
  • 浏览: 36314 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Rails中的check_box的List的一个应用

阅读更多

 

面试时遇到的一个问题,在产品列表前加个上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

 

 

分享到:
评论
1 楼 chen_miao 2012-01-13  
作为初学者的我,正用到这个,谢谢了

相关推荐

Global site tag (gtag.js) - Google Analytics