2011-04-12 41 views
1

因此,我正在製作一個程序,其中包含一個腳本「完成任務的步驟集」,可以適用於所有商店或僅適用於部分少數人。 all stores選項是一個布爾值,與腳本一起存儲。我希望這樣做的是,如果對所有商店選擇「是」,則應刪除與該腳本關聯的所有商店(如果有)。腳本和商店表具有HABTM關係。問題是如何在參數內部檢查all_stores的值是否爲真?我已經嘗試了一些東西,它似乎只是忽略if語句到所有商店參數與真實如何比較Rails表單參數?

腳本控制器

def update 
    params[:script][:store_ids] ||= [] 
    if params[:script][:all_stores] == true 
     params[:script][:store_ids] = [] 
    end 
    @script = Script.find(params[:id]) 
    if @script.update_attributes(params[:script]) 
     flash[:notice] = 'Script was successfully updated' 
     redirect_to :action => 'index' 
    else 
     @categories = category_search 
     render :action => 'edit' 
    end 
    end 

腳本編輯視圖

<%= form_for(:script, :url => {:action => 'update', :id =>@script.id}) do |f| %> 
    <div id="script_form_visibility"> 
     <div class="issue_section_header" align="center">Visibility</div> 
     <div class="line-break"></div> 
     <div class="standardText"><span class="boldText">All Stores:</span> <%=f.radio_button(:all_stores, true)%> Yes <%=f.radio_button(:all_stores, false)%> No</div> 
     <br/> 
     <div class="issue_section_header" align="center">Stores</div> 
     <div class="line-break"></div> 
     <div class="standardText"> 
      <%@stores.each do |store|%> 
       <%= check_box_tag 'script[store_ids][]', store.id, @script.store_ids.include?(store.id), :id => dom_id(store) %> 
       <%= label_tag dom_id(store), store.name, :class => "check_box_label" %><br/> 
      <%end%> 
     </div> 
    </div> 
    <div id="script_form"> 
     <div class="boldText"><%= f.label :name %></div> 
     <div><%=f.text_field :name, :size => '94', :maxlength => '70'%></div> 
     <div> 
      <table width="100%" cellspacing="0"> 
       <tr> 
        <td class="boldText"><%= f.label :category_id, "Category" %></td> 
        <td class="boldText" align="right">Show ID Required Field</td> 
       </tr> 
       <tr> 
        <td class="standardText"><%=f.select(:category_id, @categories.collect {|c| [c.name, c.id]}, :selected => session[:admin_category])%></td> 
        <td class="standardText" align="right"><%=f.radio_button(:require_id, true)%> Yes <%=f.radio_button(:require_id, false)%> No</td> 
       </tr> 
      </table> 
     </div> 
     <div class="boldText"><%= f.label :task %></div> 
     <div><%= f.text_area(:task, :size => "68x20") %></div> 
     <div class="boldText"><%= f.label :expected_results, "Expected Results" %></div> 
     <div><%= f.text_area(:expected_results, :size => "68x20") %></div> 
     <div align="center"><%= f.submit "Update Script" %></div> 
    </div> 
<% end %> 

回答

0

像這樣的問題很容易在rails調試器中解決。火起來,然後在該行設置斷點:

if params[:script][:all_stores] == true 

當你到達斷點比如你PARAMS哈希的內容

(rdb:3) p params[:script][:all_stores] 

這會告訴你爲什麼你的代碼不作爲行爲預期。

+0

謝謝你應該一直如果參數[:腳本] [:all_stores] ==「真」 – Kyle 2011-04-12 06:09:10