[英]Change GET to POST Ruby on Rails
我有一個商機模型。 當我在新表單視圖中單擊“創建機會”按鈕時,終端顯示我發出了“獲取”請求。 此外,單擊它之后,它不會運行任何驗證腳本,也不會創建新記錄。 我使用了“ rake路由”,發現我的new_opportunite路由是一個GET請求:
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 %>
您是否正在使用表單創建此表單? 如果是這樣,您應該使用[form_for][1] @var_name
,它將創建一個表單,並在提交時將POST
到正確的路徑。 您也可以選擇為我相信的鏈接指定method: :post
,但這是不正確的做法,因為鏈接應為GET
請求而不是POST
。
您的問題中有幾件事:
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
提交表單時,您不會被路由到新操作,但是會被路由到資源控制器的create操作
如果您查看耙路的輸出
POST /opportunities(.:format) opportunities#create
new_opportunity GET /opportunities/new(.:format)
當表單在瀏覽器窗口上呈現時,將調用第二條路線new_opportunity GET /opportunities/new(.:format)
,您也可以在瀏覽器的地址欄上檢查它,而第一條路線POST /opportunities(.:format) opportunities#create
是提交表單時會調用的一種方法 ,它將帶您進入商機#create
那不是您所看的正確路線。 您沒有正確配置路由,或者您看錯了路由。 路線new_opportunity GET /opportunities/new(.:format) opportunities#new
是呈現表單的路線。 發布表單的路線如下所示;
opportunities POST /opportunities(.:format) opportunities#create
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.