控制器:
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&id=75f90119-a071-470e-90cd-c04edfe02339&commit=Go
然而,我需要:
http://localhost:3000/candidates/75f90119-a071-470e-90cd-c04edfe02339
以到达正确的路线。
我哪里错了?
它看起来像这个
candidate_path(:id)
应该给@candidate
而不是id。
candidate_path(@candidate)