我有一个应该发送POST请求的Rails应用程序,但由于某种原因发送了GET。
查看:
<% if @competition.users.exclude?(@user) %>
<%= link_to 'Attend Competition', attend_competition_path(@competition.id), :method => :post %>
<% else %>
<%= link_to 'Withdraw', withdraw_competition_path(@competition.id), :method => :post %>
<% end %>
控制器:
def attend
p current_user.daily
@competition = Competition.find(params[:id])
if @competition.users.include?(current_user)
flash[:error] = "You're already attending this competition."
elsif current_user.daily == []
flash[:error] = "You must have a working device to compete."
else
current_user.competitions << @competition
flash[:success] = "Attending competition!"
end
redirect_to @competition
end
def withdraw
p "WITHDRAWING"
@competition = Competition.find(params[:id])
p @competition
attendee = Attendee.find_by_user_id_and_competition_id(current_user.id, @competition.id)
if attendee.blank?
flash[:error] = "No current attendees"
else
attendee.delete
flash[:success] = 'You are no longer attending this competition.'
end
p attendee
redirect_to @competition
end
路线:
resources :competitions do
post 'attend', on: :member
end
resources :competitions do
member do
post 'withdraw'
end
end
所以我点击按钮然后转到页面,但是收到一个错误,即没有GET请求的路由。不应该有获取请求的路由,但它应该发送帖子。
ActionController::RoutingError (No route matches [GET] "/competitions/1/withdraw")
答案 0 :(得分:0)
您可以做的一件事就是:
rake routes
这将显示您所有可用的路线及其方法。我相信,因为你正在为一个方法做一个帖子,然后创建它不理解你想要做什么。我想看看我是否能找到正确的方法来做到这一点,但我确实发现Rails文档说:
如果您依赖于POST行为,则应使用请求对象的方法进行发布?,删除?,:补丁或放置?来检查控制器的操作。<\ n / p>
因此,您可能需要检查控制器操作中的帖子。我找了一个如何做到这一点但无法找到任何东西的例子。
查看您的路线,它应该按照您的方式工作。另一件要尝试的是使用&#34; put&#34;而不是&#34;发布。&#34;
您可能需要考虑的另一个选项是将其设置为表单,并将该按钮设置为链接,如果这是您要查找的外观。
Mike Riley