2016-02-19 52 views
0

我有一個在mypage.html.erb複選框的列表。當我選中一個方框並單擊「提交」按鈕時,我想要在下一頁(bookfinal.html.erb)中選中要打印的內容的值。出於某種原因,檢查的值不打印?在控制檯中,選中的值顯示爲'1',所以我不確定丟失了什麼。如您所見,我添加了@checked = "Test"以查看該值是否會打印,但它仍然沒有?SubmitForm檢查值不張貼到頁面

mypage.html.erb

<div class="infoheaders">1. Question 
    <%= simple_form_for @category, :url => url_for(:action => 'bookfinal', :controller => 'users'), :method => 'post' do |f| %> 
    <%= f.collection_select(:title, Category.all, :title, :title) %> 
    <div class="grey-text-subheader">Device: (optional)</div> 
    <%= f.check_box :Apple %> <%= f.label "Apple" %> &nbsp; &nbsp; 
    <%= f.check_box :iPhone %> <%= f.label "iPhone" %> &nbsp; &nbsp; 
    <%= f.check_box :iPad %> <%= f.label "iPad" %> &nbsp; &nbsp; 
    <%= f.check_box :Mac %> <%= f.label "Mac" %> &nbsp; &nbsp; 
    <%= f.check_box :Android %> <%= f.label "Android" %><br><br> 
    <%= f.check_box :Samsung %> <%= f.label "Samsung" %> &nbsp; &nbsp; 
    <%= f.check_box :Microsoft %> <%= f.label "Microsoft" %>&nbsp; &nbsp; 
    <%= f.check_box :Windows %> <%= f.label "Windows" %> &nbsp; &nbsp; 
    <%= f.check_box :Google %> <%= f.label "Google" %> 
    <%= f.button :submit, 'Submit' %> 
    <% end %> 
</div> 

users_controller.rb

def bookfinal 
    if params[:category][:ipad].present? 
    @checked = params[:category][:ipad] 
    end 

    if params[:category][:android].present? 
    @checked = params[:category][:android] 
    end 
    if params[:category][:samsung].present? 
    @checked = params[:category][:samsung] 
    end 
    @checked = "Test" 
end 

bookfinal.html.erb

<div> 
    The checked value is <%= @checked %> 
</div> 

的routes.rb

Rails.application.routes.draw do 
    get 'categories/new' 
    get 'sessions/new' 
    post 'categories' => 'categories#create' 
    get 'users/edit' 
    get 'users/new' 
    get 'users/index' 
    get 'users/mypage' 
    post "users/bookfinal" => 'users#bookfinal' 
    resources :users 
    resources :categories 

控制檯

Started POST "https://stackoverflow.com/users/bookfinal" for 96.49.105.234 at 2016-02-19 21:18:21 +0000 
Processing by UsersController#bookfinal as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"zaPn5YuBFYr9VwPDSNMeA7tfm74sjM8HZneG+PCX9HEPEH/mb2qgA4xDacI9YDgcjDlFgKCQFFgWnUs5AZB0MA==", "category"=>{"title"=>"Digital Marketing", "Apple"=>"0", "iPhone"=>"0", "iPad"=>"1", "Mac"=>"0", "Android"=>"0", "Samsung"=>"0", "Microsoft"=>"0", "Windows"=>"0", "Google"=>"0"}, "commit"=>"Submit"} 
    Rendered users/bookfinal.html.erb within layouts/application (0.6ms) 
Completed 200 OK in 115ms (Views: 113.8ms | ActiveRecord: 0.0ms) 
+0

寫'的routes.rb '請和服務器日誌 – Ilya

+0

@Ilya看到路由和控制檯發佈。 – Brittany

+0

好像沒問題..試着保存你的編輯和重啓服務器 – Ilya

回答

1

更改bookfinal方法的代碼如下:

@checked = params[:category].select{|k,v| !v.to_i.zero?}.keys.join 
+0

你是...真棒。謝謝。使用這種方法,我可以重複使用選中的值嗎?例如。一旦它們出現在bookfinal.html.erb上,我可以將它們包含在此頁面上的新提交表單中(這將不可避免地通過電子郵件發送給管理員)? – Brittany

1

對於當前的PARAMS,你可以做這樣的事情:

@checked = params[:category].except(:title).select { |brand, value| value == '1' }.keys.to_sentence 

這將需要從PARAMS [:category],只留下選擇然後從品牌名稱中作出一個句子。 要選擇什麼時候處理的情況下,你可以使用這個:

@checked = 'None' if @checked.blank? 

有兩種使用形式對象等清潔解決方案,但這應該工作太

+0

我用上面的代碼替換了def bookfinal中的內容,但仍然沒有在我的bookfinal.html.erb頁面上打印。 – Brittany

0

你是什麼意思打印值意思?

如果您從params[:category][:ipad]實例中獲得01,則所有內容均按預期工作,checkboxes rendered from the form helper return either 0 or 1 if they are unchecked or checked respectively

不能獲得複選框標籤文本的方式。請求發送的是html input元素的值,並且由於我發佈的上述鏈接中解釋的原因,rails實際上將其綁定到隱藏字段(保留0或1)。

所以基本上你有兩種選擇:

  1. 創建隱藏字段來保存你可以實際發送的請求,併爲您的佔位符下頁或使用此類文本的文本...
  2. 只需檢查你的下一個頁面請求的實際值和設定的變量,你再使用,以檢查是否打印出某些標籤

我會去選擇2號,所以你會做些什麼像這樣:

在你的控制器:

def bookfinal 
    if params[:category][:ipad] 
    @ipad_checked = true 
    end 
end 

(你也可以簡單地存儲值上flash storage

,然後在bookfinal.html.erb觀點:

<div> 
<% if @ipad_checked %> 
<span>iPad is checked</span> 
<% end %> 
</div> 
+0

試過這個 - 仍然沒有結果印在bookfinal.html.erb上:/ – Brittany

+0

@Brittany如果你在視圖中輸出變量「@ipad_checked」的值,會發生什麼?輸出是什麼? –

0

這裏,從params哈希表,我們一看就知道,在iPad的檢查值是「1」,關鍵是iPad的,而不是iPad的,請更換控制器對此。

def bookfinal 
 
    if params['category']['iPad'].present? 
 
    @checked = 'Ipad is checked' 
 
    end 
 

 
    if params['category']['Apple'].present? 
 
    @checked = 'Apple is checked' 
 
    end 
 
    if params['category']['Samsung'].present? 
 
    @checked = 'Samsung is checked' 
 
    end 
 
    if params['category']['Microsoft'].present? 
 
    @checked = 'Microsoft is checked' 
 
    end 
 
    if params['category']['Android'].present? 
 
    @checked = 'Android is checked' 
 
    end 
 
    if params['category']['Mac'].present? 
 
    @checked = 'Mac is checked' 
 
    end 
 
    if params['category']['iPhone'].present? 
 
    @checked = 'iPhone is checked' 
 
    end 
 
    if params['category']['Windows'].present? 
 
    @checked = 'Windows is checked' 
 
    end 
 
    if params['category']['Google'].present? 
 
    @checked = 'Google is checked' 
 
    end 
 
    @checked = "Test" 
 
end

你bookfinal.html.erb是,

<div> 
 
    <%= @checked %> 
 
</div>

完蛋了。完成:-)

這是您的PARAMS哈希和@Category對象,

params = {"category"=>{"title"=>"Digital Marketing", "Apple"=>"0", "iPhone"=>"0", "iPad"=>"1", "Mac"=>"0", "Android"=>"0", "Samsung"=>"0", "Microsoft"=>"0", "Windows"=>"0", "Google"=>"0"}} 
 

 
if params['category']['iPad'].present? 
 
    @category = "Ipad checked" 
 
    end 
 
=> "Ipad checked" 
 

 
@category 
 
=> "Ipad checked" 
 

+0

當我在bookfinal.html.erb上實現上述內容時,我得到的所有內容是:Test/*您將收到消息什麼是選中的*? – Brittany

+0

刪除那些註釋行'/ *你會得到消息什麼是檢查*?'不可寫。這些都是評論,我已經寫出了他們的理解。從任何方式我都刪除它們。 – Sravan