Rails 在应该是 POST 时发送 GET 请求 [英] Rails sends GET request when it should be POST

查看:35
本文介绍了Rails 在应该是 POST 时发送 GET 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应该发送 POST 请求的 Rails 应用,但由于某种原因发送了 GET.

I have a Rails app that is supposed to send a POST request, but for some reason is sending a 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 请求的路由.不应该有获取请求的路由,但它应该发送一个帖子.

So I click on the button and go to the page, but get an error that there is no route for a GET request. There shouldn't be a route for get requests, but it should be sending a post.

ActionController::RoutingError (No route matches [GET] "/competitions/1/withdraw")

推荐答案

你可以做的一件事就是运行:

One thing you can do is to run:

 rake routes

这将显示您所有可用的路线及其方法.我相信,因为您正在对一种方法进行发布,然后创建它不了解您正在尝试做什么.我想看看我是否能找到正确的方法来做到这一点,但我确实发现 Rails 文档说:

That will show you all your available routes and their methods. I believe since you are doing a post to a method other then create that it is not understanding what you are trying to do. I am looking to see if I can find the proper way to do this, but I did find that the Rails documentation says:

如果您依赖 POST 行为,您应该使用请求对象的 post?、delete?、:patch 或 put? 方法在控制器的操作中检查它.

If you are relying on the POST behavior, you should check for it in your controller's action by using the request object's methods for post?, delete?, :patch, or put?.

因此,您可能需要检查控制器操作中的帖子.我查找了如何执行此操作的示例,但找不到任何内容.

So you may need to check for a post in your controller action. I looked for an example of how to do this but couldn't find anything.

看看你的路线,它应该按照你的方式工作.要尝试的另一件事是使用放置"而不是发布".

Looking at your routes, it should work the way you have it. One other thing to try is to use "put" instead of "post."

如果这是您想要的外观,您可能需要考虑的另一个选项是将其设为表单并将按钮样式设置为链接.

One other option that you may want to consider is to make it a form and style the button like a link if that is the look you are going for.

迈克·莱利

这篇关于Rails 在应该是 POST 时发送 GET 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆