2014-09-01 35 views
0

您好我有軌一種形式,這是代碼Rails的複選框,獲得所選記錄JSON值

<%= form_tag getjson_products_path do %> 
<% @products.each do |product| %> 
    <%= check_box_tag "product_ids[]",product.id , false%> 
    <%= product.name %> 
    <%= product.category %> 
    <%= product.price %> 
    <%= link_to 'Show', product %> 
    <%= link_to 'Edit', edit_product_path(product) %> 
    <%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %> 
    <br> 
<% end %> 
<%= submit_tag "submit" %> 
<br> 
<% end %> 

,我已經在產品控制器寫一個方法

def getjson 
    #stuff to do 
    redirect_to root_path 
    end 

,這是我的路線文件

resources :products do 
    collection do 
    get 'getjson' 
end 
end 

我想要的是獲得選定產品的json值,但每當我點擊提交它說路線呃請問我必須做什麼以及如何爲選定記錄獲取json值?

回答

1

在這樣

def getjson 
    if params[:product_ids] 
     @products = Product.find(params[:product_ids]) 
     render json: @products 
    end 
    end 

這就是控制器改變所有它會工作