接受请求按钮上的路由错误

时间:2014-03-26 07:10:10

标签: ruby-on-rails

在我的dafault页面中,我有一个按钮pending request,当我点击pending request按钮时,它会转到pending request页面,下面是{{1按钮代码

pending request

以下是<table> <tr> <td style="color: #212121;"> <div class="button"> <%= button_to "Pending Request", '/pendingrequest', { method: :get, :class => "buttonhome" } %> </div> </td> </tr> </table> 的路线:

pending request

match '/pendingrequest', to: 'static_pages#pending_request', via: 'get' 页面中,我收到了用户的请求,并且这些请求附有两个按钮pending requestaccept,如下所示:

ignore

当我点击<table width="50%"> <tr> <td align="center"> <div class="button"> <%= button_to "Accept", "/accept", { method: :post, :class => "buttonblck" } %> </div> </td> <td align= "center"> <div class="button"> <%= button_to "Ignore", '/default', { method: :get, :class => "buttonblck" } %> </div> </td> </tr> </table> 按钮时,我的请求已被接受,我正在创建方法中接受请求工作,即:

accept

但是当我点击 def create @user = Userrequest.select(:RequestFrom).where('RequestTo = ? AND IsApproved = ?', current_user.id, "0") requestfrom = @user.RequestFrom Userrequest.update_attributes('RequestFrom = ? AND RequestTo = ? AND IsApproved = ?', requestfrom,current_user.id,"1") @user_request = Userrequest.new( :RequestFrom => requestfrom , :RequestTo => current_user.id , :IsApproved => "1" , :SkillType => "" ) @user_request.save redirect_to '/default' end 按钮时,会出现以下错误:

accept

但是 No route matches [POST] "/pendingrequest" "/pendingrequest"页面的网址,为什么它会在pending request按钮上显示此错误。以下是accept按钮的路线:

accept

请帮助我,等你回复。 感谢

1 个答案:

答案 0 :(得分:0)

首先,您希望将path helper用于所有按钮。使用直接路径不是很干(如果你改变路线,你将来会遇到问题):

<%= button_to "Pending Request", pendingrequest_path, { method: :get, :class => "buttonhome" } %>

虽然我不完全确定导致错误的原因,但我会考虑以下三个方面进行测试:


button_to vs link_to

button_to创建一个表单,每次单击该按钮时都会提交该表单。这是相对不可靠的(特别是如果你包含在另一个表格中等),所以最好使用link_to进行测试,看看是否是错误:

<%= link_to "Pending Request", '/pendingrequest', method: :get %>

<强>路线

其次,您的路线需要修复

我会推荐这种结构:

#config/routes.rb
static = %w(pendingrequest create accept)
for page in static do
    get page, to: "static_page##{page}"
end

<强>方法

最后,你需要确定你的方法

Rails introduced the HTTP verb给很多程序员 - 这意味着你需要确保知道你应该使用哪种动词(方法)

如果您只是请求一个页面(get),您的路线应该反映出来。 POST主要用于提交数据,而不是请求:)