尽管使用POST,URL参数仍显示在地址栏中

时间:2012-08-21 13:26:50

标签: ruby-on-rails-3

我是ROR的新手,但我遇到了这个问题: 在索引页面我有

<%= button_to 'Yes', { :action => 'vote', :id => poll.id, :user_answer => 'yes' }, :method => :post  %>

当用户按下“是”按钮时,传递给控制器​​的URL包含显式给用户的所有参数。

vote?id=1&user_answer=yes
在routes.rb中

我有:

match 'vote' => 'polls#vote', :via => :post 

感谢任何帮助

编辑:整个index.html.erb     

投票

 <% @polls.each do |poll| %>
   <p>
   <%= poll.question %>?
   <%= button_to 'Yes', { :action => 'vote', :id => poll.id, :user_answer => 'yes' }, :method => :post %> (<%= poll.yes %>) /
   <%= button_to 'No', { :action => 'vote', :id => poll.id, :user_answer => 'no' }, :method => :post %> (<%= poll.no %>)
   </p>
 <% end %>

1 个答案:

答案 0 :(得分:0)

button_to的默认方法是post,所以除非你想使用其他动词(get,put),否则不需要显式指定它。

如果您不喜欢附加到网址的参数,则可能需要使用表单 或者,只需将表单哈希添加到button_to标记:

<%= button_to 'Yes', { :action => 'vote', :id => poll.id, :user_answer => 'yes' }, :form => {:data_type => <your choice (html, json)> %>