【问题标题】:How to only get id from params in select form to redirect to user show page?如何仅从选择表单中的参数获取 id 以重定向到用户显示页面?
【发布时间】:2015-07-01 05:38:27
【问题描述】:

控制器:

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) if current_user
    @contact = ContactCandidate.new(candidate_email: @candidate.email)
  end

  def send_unregistered
    @candidate = Candidate.find(params[:id])
    if @candidate.send_unregistered(params[:contact_candidate])
      flash[:error] = nil
      flash[:notice] = "You have emailed #{@candidate.full_name} an invitation to Votus"
    else
      flash[:error] = "Your invitation could not send."
    end
    redirect_to candidate_path(@candidate)
  end
end

在视图中选择表单:

      - if current_user
        = form_tag candidate_path(:id), :method => :get do
          = select_tag :id, options_from_collection_for_select(@candidates, :slug, :full_name), :style => "width:100%; margin-left: 3px;"
          = submit_tag 'Go'

所以从我上面的代码中,选择表单将我带到http://localhost:3000/candidates/id?utf8=%E2%9C%93&amp;id=75f90119-a071-470e-90cd-c04edfe02339&amp;commit=Go

但是,我需要:

http://localhost:3000/candidates/75f90119-a071-470e-90cd-c04edfe02339

到达正确的路线。

我哪里错了?

【问题讨论】:

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


    【解决方案1】:

    看起来像这样

    candidate_path(:id)

    应该给定@candidate而不是id。

    candidate_path(@candidate)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-12
      • 1970-01-01
      • 2015-08-29
      • 2017-10-05
      • 1970-01-01
      • 1970-01-01
      • 2012-07-30
      相关资源
      最近更新 更多