2015-07-02 99 views
0

我有這個控制器與此動作:如何在rails中設置select_tag的默認值?實例變量

class CandidatesController < ApplicationController 
    helper_method :current_or_guest_user 

    def show 
    # authorize! :read, @user 
    @candidate = Candidate.friendly.find(params[:id]) 
    @candidates = Candidate.all 
    if request.path != candidate_path(@candidate) 
     redirect_to @candidate, notice: 'moved permanently' 
    end 
    @comparison = UserComparisonService.new(current_user, @candidate) 
    @contact = ContactCandidate.new(candidate_email: @candidate.email) 
    end 

這需要一個局部呈現顯示頁面上,並傳遞@candidate實例變量:

= render 'users/compare', :candidate => @candidate 

這是內形式部分:

profile-searchbar.text-left 
     = form_tag(compare_path, method: 'get', remote: true, id: 'compare_form') do 
      = select_tag "v_friend_id", options_from_collection_for_select(@candidates, :id, :full_name, @comparison.v_friend.id), :include_blank => true, :class => "compare-search", :style => "width:100%; margin-left: 3px 

%a.compare_btn{:href => "javascript:void(0)"} 

此選擇dropdo wn適用於控制器中的candidate.All方法,但是如何將候選人的默認值設置爲@candidate,因此我可以取消下拉/搜索並僅對候選人的頁面進行比較?

回答

0

這是從APIdock

select_tag "people", options_from_collection_for_select(@people, "id", "name", "1") 

,輸出是:

<select id="people" name="people"><option value="1" selected="selected">David</option></select> 

正如你所看到的,id作爲第四個元素傳遞options_from_collection_for_select所以不是@comparison.v_friend.id我想你必須使用@candidate.id