2014-03-26 89 views
0

在我dafault頁我有一個按鈕pending request,當我在pending request按鈕點擊路由錯誤,那麼它去我的pending request頁面,下面是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 request頁,我收到來自用戶和隨着次請求ESE要求兩個按鈕acceptignore是即低於附:

<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按鈕點擊,我的請求已被接受,我做接受create方法要求工作即低於:

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按鈕的路線:

match '/accept', to: 'static_pages#create', via: 'post' 

請幫助我,等你答覆。 謝謝

回答

0

首先,你想爲所有的按鈕使用path helper。使用直接路徑是不是很乾(如果你改變了路線,你就必須在未來的問題):

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

雖然我不完全知道是什麼導致你的錯誤,我想看看3事情進行測試:


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主要用於提交數據,不要求:)