2017-01-23 56 views
1

在我的Rails 5應用程序中,我有一個簡單的表單用於爲我的模型Training創建或更新。在這個表格中,我已經有了我的每個Participant的複選框,以將它們分配給Training。現在我試圖爲每個Participant添加一個單選按鈕組(5對於1到5的評分),因爲每個按鈕組可以評分TrainingRails 5簡單表單保存模型屬性中的表單提交的哈希值

Screen shot of the rendered form

我設法使單選按鈕,因爲我想只可惜我只能選擇呈現的所有單選按鈕之一。他們甚至連接奇怪的複選框,這可能是因爲他們有相同的輸入法participant_ids,它連接到has_and_belongs_to_many協會與Participant:participants

<%= simple_form_for @training do |f| %> 
... 
    <div id="participants-ratings-container"> 
    <div id="participants-container" style="display: inline-block; vertical-align: top; width: 8em"> 
    <%= f.label t('activerecord.models.participant') %> 
    <%= f.collection_check_boxes :participant_ids, Participant.all.order(:name), :id, :name, {:item_wrapper_class => 'checkbox-container'} %> 
    </div> 

    <div id="ratings-container" style="display: inline-block; vertical-align: top"> 
    <%= f.label t('trainings._form.ratings') %> 
    <% Participant.all.order(:name).each do |p| %> 
    <%= f.input :participant_ids, collection: 1..5, as: :radio_buttons, label: false%> 
    <% end %> 
    </div> 
... 
<% end %> 

我嘗試了很多東西,但我沒有想法了。如果可能,我想以「參與者名稱」=>評分值的方式在我的Training課程中填寫哈希participant_ratings

任何人都可以給我一個關於如何實現這一點的提示嗎?如果相應的Participant未被選中,甚至可能告訴我如何禁用單選按鈕組?

在此先感謝!

UPDATE

我設法去適應我的形式,甚至整合的jQuery插件RATY進入每個Participant評定值。每個評級與其相應的參與者ID一起作爲散列發送到服務器,例如,如下所示:

"participant_ratings"=>{"3"=>"5", "1"=>"3.5", "2"=>""}. 

如果未選中相應的參與者複選框,則將禁用每個評分並將其值設置爲0。這裏是我的視圖代碼:

<div id="ratings-container" style="display: inline-block; vertical-align: top"> 
    <%= f.label t('trainings._form.ratings') %> 
    <% Participant.all.order(:name).each do |p| %> 
     <span class="checkbox_container"> 
     <% current_rating = @training.participant_ratings[p.id].nil? ? '' : @training.participant_ratings[p.id] %> 
      <label id='star<%= "#{p.id}" %>' data-rating='<%= "#{current_rating}" %>' style="margin-bottom: 12px;" ></label> 
     <input id="rating<%= "#{p.id}" %>" name="participant_ratings[<%= "#{p.id}" %>]" type="hidden" value="<%= "#{@training.participant_ratings[p.id]}" %>" /> 
     </span> 
     <script> 
      $('#star<%= "#{p.id}" %>').raty({ 
       score: function() { 
        return $(this).attr('data-rating'); 
       }, 
       half : true, 
       readOnly: <%= [email protected]?(p) %>, 
       path: '/assets', 
       click : function(score, evt) { 
        $('#rating<%= "#{p.id}" %>').val(score); 
       } 
      }); 
      $('#training_participant_ids_<%= "#{p.id}" %>').change(function() { 
       var star = $('#star<%= "#{p.id}" %>'); 
       if(document.getElementById('training_participant_ids_<%= "#{p.id}" %>').checked) { 
        star.raty('readOnly', false); 
       } else { 
        star.raty('cancel'); 
        star.raty('readOnly', true); 
        $('#rating<%= "#{p.id}" %>').val(""); 
       } 
      }) 
     </script> 
    <% end %> 
</div>  

New screen shot of the rendered form

我似乎不過去的事情才能夠搞清楚的是如何保存participant_ratings散列相應的模型屬性:participant_ratings

如果我允許參數:participant_ratings在我的trainings_controller.rb中,但沒有保存。所以我添加

@training.participant_ratings = params[:participant_ratings] 

,現在我得到的錯誤

Attribute was supposed to be a Hash, but was a ActionController::Parameters. -- <ActionController::Parameters {"3"=>"5", "1"=>"3.5", "2"=>""} permitted: false> 

trainings_controller.rb方法:

class TrainingsController < BaseController 
... 
    def update 
    @training = Training.find(params[:id]) 
    @training.updated_date_time = DateTime.now 
    @training.participant_ratings = params[:participant_ratings] 

    if @training.update(training_params) 
     flash[:notice] = t('flash.training.updated') 
     redirect_to @training 
    else 
     render 'edit' 
    end 
    end 

    private 
    def training_params 
     params.require(:training).permit(:village, :topic, :user_id, :start_time, :end_time, {:participant_ids => []}, :participant_ratings) 
    end 
end 

如何保存由形式模型的發送哈希屬性?我哪裏錯了?
我可以以某種方式將participant_ratings定義爲類似於數組participant_ids的參數中的散列嗎?

回答

0

首先,我必須確保當我的表單提交的participants_rating哈希是訓練哈希裏面,所以我不得不改變name屬性在我隱藏的表單輸入評級:

<input id="rating<%= "#{p.id}" %>" name="training[participant_ratings][<%= "#{p.id}" %>]" type="hidden" value="<%= "#{@training.participant_ratings[p.id]}" %>" /> 

其次,我有一個白名單的按鍵我participant_ratings哈希,使他們被允許:

def training_params 
    ratings_keys = params[:training].try(:fetch, :participant_ratings, {}).keys 
    params.require(:training).permit(:village, :topic, :user_id, :start_time, :end_time, {:participant_ids => []}, participant_ratings: ratings_keys) 
end 

我發現這個解決方案在this rails thread

之後,participant_ratings保存得很好!