2012-03-22 21 views
0

我創建一個Rails 3.1的應用程序,我已經下大問題:我必須檢索從視圖中的某些參數控制器檢索複雜的參數從視圖中的Rails 3.1

這是我的看法:

<tr class="<%= cycle("odd", "even") %>"> 

       <td><%= text_field_tag("bulk_warehouse_serial#{@count}_page#{params[:page]}", bulk_warehouse.serial, :disabled => true) %></td> 
       <td><%= text_field_tag("bulk_warehouse_asset#{@count}_page#{params[:page]}", bulk_warehouse.asset, :disabled => true)%></td> 




      <td><%= check_box_tag "enable_record#{@count}_page#{params[:page]}",1,false,{:onclick => "bulk_warehouse_serial#{@count}.disabled = 
                      bulk_warehouse_asset#{@count}.disabled = 
                      !this.checked;"}%></td> 
       <td class="last"> 
      <%= link_to "#{t("web-app-theme.delete", :default => "Delete")}", bulk_warehouse_path(bulk_warehouse), :method => :delete, :confirm => "#{t("web-app-theme.confirm", :default => "Are you sure?")}" %> 
      </td> 


     </tr> 
     </div>   
<% @count = @count +1 %> 

在我控制我有一些諸如:

... 
    @count = 0 
    ... 

,這是由Web服務器日誌生成的內容:

"warehouse"=>{"asset"=>"turiturira", "serial"=>"caricarira", "project_id"=>"1", "hardware"=>{"brand_id"=>"21"}, "hardware_id"=>"60", "state_id"=>"4", "position_id"=>"1", "logicalwarehouse_id"=>"3", "extra_id"=>"3"}, "bulk_warehouse_serial270"=>"t", "bulk_warehouse_asset270"=>"test", "enable_record2_page0"=>"1", "bulk_warehouse_serial2"=>"uela2", "bulk_warehouse_asset2"=>"bela2", "enable_record3_page0"=>"1"}_ 

e 現在,在我的控制器中,我需要一個首先檢查所有「enable_record#{@ count} _page#{params [:page]}」值的操作,然後執行一些操作。我如何在控制器中聲明?我在想,如:

@count=0 
@page = params[:page] 
@count_max = 10 
until @count == @count_max 

if params[:warehouse][:enable_record#{@count}_page#{params[:page]}] == 1 
...doing something... 
end 
@[email protected]+1 
end 

但它給了我一個驚喜;有什麼建議麼?

+0

你得到的錯誤是什麼? – miked 2012-03-22 14:42:56

回答

1

這似乎很複雜,無論你想要做什麼,但我會避免重構你的方法。然而,代碼中有一個突出的特點 - 它是您使用的密鑰的動態構造。你可能想用一個字符串構造它然後'符號'它:

key = "enable_record#{@count}_page#{params[:page]}".to_sym 
if params[:warehouse][key] == 1 
...doing something... 
end 

注意:你也可能會重新考慮平等條件。散列的值可能不是整數1,因爲您的代碼建議並且實際上可能是「1」(一個字符串)。如果存在任何不確定性,請確保兩個值的類型相同(使用.to_s或.to_i)。