2015-08-24 204 views
0

我有一個窗體內的項目視圖,它允許用戶給該項目的錢。我已經建立了資源,將每筆付款與相應的項目相關聯。我無法將表單提交到正確的路徑。在routes.rb中我有這樣的:嵌套資源和form_for

resources :projects do 
    resources :payments, only: [:new, :create, :show] 
end 

形式看起來是這樣的:

<div class="container"> 
<div class="row Row one"> 
    <div class="col-sm-12 col-md-10"> 
     <%= form_for [@projects, @payments] , method: 'post' do %> 

       <%= label_tag("First Name") %><br> 
       <%= text_field(:user, :first_name, class: 'form-control', :required => true) %><br> 

       <%= label_tag("Last Name") %><br> 
       <%= text_field(:user, :last_name, class: 'form-control', :required => true) %><br> 

       <%= label_tag("Card Number") %><br> 
       <%= text_field(:user, :cc_num, class: 'form-control', :required => true)%><br> 

       <%= label_tag("Expiration Month") %><br> 
       <%= text_field(:user, :exp_month, class: 'form-control', :required => true)%> 

       <%= label_tag("Expiration Year") %><br> 
       <%= text_field(:user, :exp_year, class: 'form-control', :required => true)%>  

       <%= label_tag("Amount") %><br> 
       <%= text_field(:user, :amount, class: 'form-control', :required => true)%> 

       <%= hidden_field_tag :user_id, :value => current_user.id , class: 'form-control'%> 
       <%= hidden_field_tag :project_id, :value => @project.id , class: 'form-control'%> 

       <%= submit_tag("Sign Up") %> 
       <% end %> 
    </div> 
</div> 
<!-- <div class="row"></div> --> 

我已經試過包裝在括號中的數組,我也曾嘗試:

<% form_for @projects, :url => {:action => 'create', :id => @project.id }, :html => { :method => :post } do |f| %> 

正如我在其他線程中看到的,但這不適用於我。耙路線顯示的路徑應該是:

project_payments POST /projects/:project_id/payments(.:format)  payments#create 

我得到的錯誤是:

No route matches [POST] "/projects/1" 

我應該如何路由這種形式?謝謝!

回答

0

是在我的情況下,更需要明確的形式的頭:

<%= form_for [@project, @payment], url: project_payments_path(@project.id) , method: 'post' do %>