2014-07-02 48 views
0

我有一個機會模型。當我在我的新表單視圖中點擊我的「創建機會」按鈕時,我的終端說我做了一個「GET」請求。此外,單擊它後,它不會運行任何驗證腳本,也不會創建新記錄。我用「回扣路線」,看到我的new_opportunite路線是一個GET請求:更改GET到POST Ruby on Rails

new_opportunity GET /opportunities/new(.:format)機會#新

有誰知道我可以改變這條路線,使它是一個POST?

謝謝!

耙路線:

  Prefix Verb URI Pattern             Controller#Action 
      opportunities GET /opportunities(.:format)          opportunities#index 
         POST /opportunities(.:format)          opportunities#create 
     new_opportunity GET /opportunities/new(.:format)         opportunities#new 
     edit_opportunity GET /opportunities/:id/edit(.:format)       opportunities#edit 
      opportunity GET /opportunities/:id(.:format)         opportunities#show 
         PATCH /opportunities/:id(.:format)         opportunities#update 
         PUT /opportunities/:id(.:format)         opportunities#update 
         DELETE /opportunities/:id(.:format)         opportunities#destroy 

路線文件:

Rails.application.routes.draw do 

    resources :test_users 

    get 'profile/my_profile' 

    get "log_out" => "sessions#destroy", :as => "log_out" 

    get "log_in" => "sessions#new", :as => "log_in" 

    get "sign_up" => "users#new", :as => "sign_up" 
    root :to => "sessions#new" 

    get 'view_submitted/submitted' 

    get 'view_action_list/seven' 

    get 'show_number/thirty' 

    get 'show_number/sixty' 

    get 'show_number/year' 

    get 'view_retired/retired' 

    resources :users 
    resource :sessions 

    get 'report/report_page' 

    resources :opportunities do 
    resources :activities 
    resources :updates 
    resources :contacts 
    resources :links 

    end 

我的表格:

<%= form_for(@opportunity) do |f| %> 
     <% if @opportunity.errors.any? %> 
     <div id="error_explanation"> 
      <h2><%= pluralize(@opportunity.errors.count, "error") %> prohibited this opportunity from being saved:</h2> 
      <ul> 
      <% @opportunity.errors.full_messages.each do |message| %> 
      <li><%= message %></li> 
      <% end %> 
      </ul> 
     </div> 
     <% end %> 


     <table id="new_opp_form" style="width: 450px; background-color: white; border-style: none; border:1px orange;"> 
      <tr style="background-color: white"> 
      <td>Department</td> 
      <td><%= f.select :department, [[],['DHS', 'DHS'], ['DOS', 'DOS'], ['FDIC', 'FDIC'], ['HUD', 'HUD'], ['SEC', 'SEC'], ['VA', 'VA']] %></td> 
      </tr> 
      <tr style="background-color: white"> 
      <td>Agency</td> 
      <td><%= f.text_field :agency %></td> 
      </tr> 
      <tr style="background-color: white"> 
      <td>Program Name</td> 
      <td><%= f.text_field :program_name %></td> 
      </tr> 
      <tr style="background-color: white"> 
      <td>Stage</td> 
      <td><%= f.select :stage, [[],['Assessment', 'Assessment'], ['Pre-Proposal', 'Pre-Proposal'], ['Proposal', 'Proposal'], ['Subitted', 'Submitted'],['Retired', 'Retired']] %></td> 
      </tr> 
      <tr style="background-color: white"> 
      <td>Capture Manager</td> 
      <td><%= f.collection_select(:capture_manager, User.all, :full_name,:full_name,{:prompt => true}) %></td> 
      </tr> 
      <tr style="background-color: white"> 
      <td>Program Description</td> 
      <td><%= f.text_area :program_description %></td> 
      </tr> 
      <tr style="background-color: white"> 
      <td>New or Re-recompete</td> 
      <td><%= f.select :new_or_recompete, [[],['New', 'New'], ['Re-Compete', 'Re-Compete']] %></td> 
      </tr> 
      <tr style="background-color: white"> 
      <td>Incumbent</td> 
      <td><%= f.text_field :incumbent %></td> 
      </tr> 
      <tr style="background-color: white"> 
      <td>Current Contract Vehicle</td> 
      <td><%= f.text_field :curent_contract_vehicle %></td> 
      </tr> 
      <tr style="background-color: white"> 
      <td>New Contract Vehicle</td> 
      <td><%= f.text_field :new_contract_vehicle %></td> 
      </tr> 
      <tr style="background-color: white"> 
      <td>Small Business Set Aside?</td> 
      <td><%= f.select :sb_set_aside, [[],['Yes', 'Yes'], ['No', 'No']] %></td> 
      </tr> 
      <tr style="background-color: white"> 
      <td>Prime or Sub</td> 
      <td><%= f.select :prime_or_sub, [[],['Prime', 'Prime'], ['Sub', 'Sub']] %></td> 
      </tr> 
      <tr style="background-color: white"> 
      <td>NAICS</td> 
      <td><%= f.text_field :naics %></td> 
      </tr> 
      <tr style="background-color: white"> 
      <td>RFI Date</td> 
      <td><%= f.date_select :rfi_date, {:include_blank => true, :default => nil} %></td> 
      </tr> 
      <tr style="background-color: white"> 
      <td>RFI Submitted?</td> 
      <td><%= f.select :rfi_submitted, [[],['Yes', 'Yes'], ['No', 'No']] %></td> 
      </tr> 
      <tr style="background-color: white"> 
      <td>Est. RFP Date</td> 
      <td><%= f.date_select :est_rfp_date, {:include_blank => true, :default => nil} %></td> 
      </tr> 
      <tr style="background-color: white"> 
      <td>Est. Full Value</td> 
      <td><%= f.text_field :est_full_value%></td> 
      </tr> 
      <tr style="background-color: white"> 
      <td>Est. Workshare (%)</td> 
      <td><%= f.text_field :est_workshare %></td> 
      </tr> 
      <tr style="background-color: white"> 
      <td>P(win) %</td> 
      <td><%= f.text_field :p_win %></td> 
      </tr> 
      <tr style="background-color: white"> 
      <td>Govwin ID</td> 
      <td> <%= f.text_field :govwin_id %></td> 
      </tr> 
     </table> 
     <div class="actions"> 
     <%= f.submit %> 
     </div> 
    <% end %> 
+0

問題不在您的路線。 – zishe

回答

1

您使用的形式來建立這個?如果是這樣,你應該使用[form_for][1] @var_name這將創建一個表格,當提交它會POST到正確的路徑。您也可以選擇指定method: :post作爲我相信的鏈接,但這是不好的做法,因爲鏈接應該是GET請求,而不是的。

+0

我剛剛添加了我的表單...我相信它是以您提到的格式顯示的。 –

+0

默認情況下,創建新記錄時,窗體應始終處於POST狀態。如果您打開chromes開發工具並在提交時觀看網絡請求,它會顯示一個帖子? – CWitty

1

有你的問題兩件事情:

When I click my "Create Opportunity" button in my new form view, my terminal says that I made a "GET" request

形式作出缺席一個post request,如果您有類似

<%= form_for @person do |f| %> 
    #your fields 
<% end %> 

這將產生

<form action="/people" class="new_person" id="new_person" method="post"> 
    //your fields 
</form> 

Furthermore, after I click it, it does not make any run any of my validation scripts nor does it create a new record. I used "rake routes" and saw that my new_opportunite route is a GET request

當你提交表單你是不是路由到您的新的行動,但如果你看看你的耙路輸出你被路由到您的資源控制器

的創建操作

POST /opportunities(.:format)  opportunities#create 
new_opportunity GET /opportunities/new(.:format) 

您的第二條路線new_opportunity GET /opportunities/new(.:format)將在您的表單在您的瀏覽器窗口呈現時調用,您可以在上檢查它瀏覽器的地址欄也和你的第一個路線POST /opportunities(.:format) opportunities#create是一個將被調用時您提交表單,它會帶你到機會#創建

0

那不是你看的正確路線。 要麼你沒有正確配置你的路線,要麼你看錯了路線。 路由new_opportunity GET /opportunities/new(.:format) opportunities#new是呈現表單的路由。 張貼表單的路線如下所示;

opportunities POST  /opportunities(.:format)        opportunities#create