缺少必需的键:[:选项]

时间:2013-07-09 18:23:32

标签: ruby-on-rails routes

我有这样的路线:默认情况下启动投票

match "poems/by_vote" => "poems#index" , via: [:get] , :as => :poems_by_votes , :vote => true

当我使用时:

<%= link_to "by_vote" , poems_by_votes_path%>

我正面对:

No route matches {:action=>"index", :controller=>"poems", :vote=>true} missing required keys: [:option]

我也使用了这些,但没有一个有效:

<%= link_to "test" , poems_by_votes_path(:vote => true)%>
<%= link_to "test" , poems_by_votes_path , :options => { :vote => true } %>
<%= link_to "test" , poems_by_votes_path(true)%>

该怎么办?

1 个答案:

答案 0 :(得分:2)

poems_by_votes_path将返回一个字符串,然后由link_to使用。 vote参数需要包含在该字符串中的某个位置(例如查询字符串中)。我会做类似的事情:

get 'poems/by_vote', as: :poems_by_vote

....

poems_by_vote_path # poems/by_vote
poems_by_vote_path(vote: true) # poems/by_vote?vote=true

....

def index
  vote = params[:vote] || false
  # look up poems based on vote...
end