如何在rails中设置select_tag的默认值?实例变量

时间:2015-07-02 02:28:16

标签: ruby-on-rails ruby-on-rails-4

我有这个操作的控制器:

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

这要求在show页面上进行部分渲染并传递@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)"}

此选择下拉列表适用于控制器中的candidate.All方法,但如何将候选项的默认值设置为@candidate,因此我可以取消下拉/搜索并将其仅与候选项进行比较#39;它的页面是什么?

1 个答案:

答案 0 :(得分: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